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
For SSN's:
http://regexlib.com/Search.aspx?k=ssn
For EINs:
http://regexlib.com/REDetails.aspx?regexp_id=1990
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>
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.
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}$)/
精彩评论