vickey
vickey

Reputation: 55

regex code to validate SSN OR Tin

I have a requirement where I need to validate a text box for SSN or Tin, I am able to achieve it if I am doing it individual but I need a regex code which will validate both the condition at a time. The format will be 111-11-1111 or 11-1111111.anything apart from this should throw error .

Please Help me to resolve it.

Upvotes: 0

Views: 8659

Answers (2)

shiva prasad patil
shiva prasad patil

Reputation: 61

Check if this could help

function validateSSNorTIN(data) {
    var ssn = (^\d{3}-?\d{2}-?\d{4}$|^XXX-XX-XXXX$);
    var tin = (^\d{2}-?\d{7}$|^XX-XXXXXXX$);

        if (ssn.test(data)||tin.test(data)) {
            setDiv(div_id, "");
            return true;
        } else {
            setDiv(div_id, "*Invalid SSN or TIN*");
            return false;

        }
    }

Upvotes: 0

Rafael
Rafael

Reputation: 18522

You could use the | (alternative) symbol

var ssnOrTinRegex = /^(?:\d{3}-\d{2}-\d{4}|\d{2}-\d{7})$/;
console.log(
    ssnOrTinRegex.test('111-11-1111'),
    ssnOrTinRegex.test('11-1111111')
);

Upvotes: 2

Related Questions