SSN Regex for 123-45-6789 OR XXX-XX-XXXX

前端 未结 6 1277
长发绾君心
长发绾君心 2020-12-04 17:43

Can someone provide me a regex for SSN that matches either

123-45-6789

OR

XXX-XX-XXXX

I currently have ^\\d{3}-?\\d{2}-?\\d{4}$

6条回答
  •  囚心锁ツ
    2020-12-04 18:17

    To strictly answer you question:

    ^(123-45-6789|XXX-XX-XXXX)$
    

    shoud work. ;-)

    If you read the section "Valid SSNs" on Wikipedia`s SSN article then it becomes clear that a regex for SSN validation is a bit more complicated.

    Accordingly a little bit more accurate pure SSN regex would look like this:

    ^(?!(000|666|9))\d{3}-(?!00)\d{2}-(?!0000)\d{4}$
    

提交回复
热议问题