最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regex for Validating SSN with * - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 6

You 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 match a or b;
  • 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
发布评论

评论列表(0)

  1. 暂无评论