I need a regex that validates a proper SSN number displaying in Screen with mask.
However we show only last 4 digits of SSN and we mask other character with *
symbol
User can either enter only a number at the place of *
or they leave as it is.
User can enter valid input or they never change
***-**-1234
123-55-1234
User changed the displayed to Invalid SSN
5**-**-1234
A**-**-1234
AAA-5*-1234
Need a JavaScript Regex code/article for this.
I need a regex that validates a proper SSN number displaying in Screen with mask.
However we show only last 4 digits of SSN and we mask other character with *
symbol
User can either enter only a number at the place of *
or they leave as it is.
User can enter valid input or they never change
***-**-1234
123-55-1234
User changed the displayed to Invalid SSN
5**-**-1234
A**-**-1234
AAA-5*-1234
Need a JavaScript Regex code/article for this.
Share Improve this question asked Jun 26, 2013 at 12:18 Murali MurugesanMurali Murugesan 22.6k18 gold badges75 silver badges122 bronze badges 1- 1 and what have you tried? why didn't it work? – jbabey Commented Jun 26, 2013 at 12:19
1 Answer
Reset to default 6You can use the regex:
(\d{3}-\d{2}|\*{3}-\*{2})-\d{4}
To allow:
***-**-1234
123-55-1234
And nothing else.
Explanation:
\d
mean a digit (that is, any number from 0 to 9 (unicode digits as well));\d{3}-\d{2}
means 3 digits, followed by the char-
followed by 2 digits;\*{3}-\*{2}
means 3 chars*
, followed by the char-
followed by 2 chars*
;|
is the alternation operator. It means roughly "or", like(a|b)
means matcha
orb
;- So:
(\d{3}-\d{2}|\*{3}-\*{2})
means (3 digits, followed by the char-
followed by 2 digits) OR (3 chars*
, followed by the char-
followed by 2 chars*
). - Finally, the whole code
(...)-\d{4}
means the same as explained in the previous item followed by the char-
followed by 4 digits.
In summary, (\d{3}-\d{2}|\*{3}-\*{2})-\d{4}
means:
- ((3 digits, followed by the char
-
followed by 2 digits) OR (3 chars*
, followed by the char-
followed by 2 chars*
)), all followed by the char-
followed by 4 digits