yogeswaran K
yogeswaran K

Reputation: 2288

Regex for EIN number and SSN number format in jquery

For jquery validation i need regex for SSN number format and EIN number format

EIN number like 52-4352452

SSN number like 555-55-5555

Upvotes: 16

Views: 35310

Answers (4)

Robert Harvey
Robert Harvey

Reputation: 180868

For SSN's:

http://regexlib.com/Search.aspx?k=ssn

For EINs:

http://regexlib.com/REDetails.aspx?regexp_id=1990

Corrected to include missing prefixes:

/^([0][1-6]|1[0-6]|(?!70)[27][0-7]|(?!31)[35][0-9]|[468][0-8]|9[0-589])?\d{7}$/

Upvotes: 22

OneNeptune
OneNeptune

Reputation: 913

As all previous answers treat this as two separate problems, I've combined the necessary regex into a single regex.

This will work for BOTH SSN and EIN, whether it's formatted with or without the use of hyphens.

/(^\d{9})|(^\d{3}-\d{2}-\d{4}$)|(^[1-9]\d?-\d{7}$)/

Upvotes: 3

fusion27
fusion27

Reputation: 2646

Our Needs

  • No prefix restrictions.
  • 9-digit EIN, hyphen optional.
  • With the hyphen, acceptable format: 00-0000000.

Regular Expression

^\d{2}\-?\d{7}$

Demonstrated with JavaScript/jQuery

<input type="text" id="ein1" value="0112345-67" />
<input type="text" id="ein2" value="01-1234567" />
<input type="text" id="ein3" value="011234567" />

<script type="text/javascript">
    var patternEIN = /^\d{2}\-?\d{7}$/;

    patternEIN.test($('#ein1').val()); // fail
    patternEIN.test($('#ein2').val()); // pass
    patternEIN.test($('#ein3').val()); // pass
</script>

Upvotes: 13

Truffula
Truffula

Reputation: 99

For EINs, this will return true if it is valid:

/^(0[1-9]|[1-9]\d)-\d{7}$/.test($('#your-ein-field-id').val())

To restrict it to actual prefixes, use this regular expression instead:

/^(0[1-6]||1[0-6]|2[0-7]|[35]\d|[468][0-8]|7[1-7]|9[0-58-9])-\d{7}$/

However, please note that if new prefixes are added this will not be up to date.

Upvotes: 3

Related Questions