Checking a Social Security number that accepts dashes, spaces or spaces

The social security numbers I want to accept are as follows:

xxx-xx-xxxx (ex. 123-45-6789)
xxxxxxxxx (ex. 123456789)
xxx xx xxxx (ex. 123 45 6789)

I'm not a regex expert, but I wrote this (it's kind of ugly)

^(\d{3}-\d{2}-\d{4})|(\d{3}\d{2}\d{4})|(\d{3}\s{1}\d{2}\s{1}\d{4})$

However, this social security number passes when it should really fail, because there is only one space

12345 6789

So I need an updated regex that rejects things like

12345 6789
123 456789

To make things more complicated, it seems that SSNs cannot start with 000 or 666 and can go up to 899, the second and third set of numbers also cannot be all 0.

I came up with this

^(?!000|666)[0-8][0-9]{2}[ \-](?!00)[0-9]{2}[ \-](?!0000)[0-9]{4}$

Which is checked with spaces or dashes, but it fails if the number looks like

123456789

Ideally, this SSN set should go through

123456789
123 45 6789
123-45-6789
899-45-6789
001-23-4567

And they must fail

12345 6789
123 456789
123x45x6789
ABCDEEEEE
1234567890123
000-45-6789
123-00-6789
123-45-0000
666-45-6789
+4
3

, , .. , . - ([ \-]?). \1, , :

^(?!000|666)[0-9]{3}([ -]?)(?!00)[0-9]{2}\1(?!0000)[0-9]{4}$

( @Tushar)

+6

CodeProject http://www.codeproject.com/Articles/651609/Validating-Social-Security-Numbers-through-Regular. , , , .

:

  • (.. 000 - ## - ####, ### - 00 - #### ### - ## - 0000)
  • 666 '.
  • 900 999
  • 078-05-1120 '(- Woolworths Wallet Fiasco)
  • Be 219-09-9999 '( )

RegEx, CodeProject, - .

^(?!219-09-9999|078-05-1120)(?!666|000|9\d{2})\d{3}-(?!00)\d{2}-(?!0{4})\d{4}$

^(?!219 09 9999|078 05 1120)(?!666|000|9\d{2})\d{3} (?!00)\d{2} (?!0{4})\d{4}$

,

^(?!219099999|078051120)(?!666|000|9\d{2})\d{3}(?!00)\d{2}(?!0{4})\d{4}$

,

^((?!219-09-9999|078-05-1120)(?!666|000|9\d{2})\d{3}-(?!00)\d{2}-(?!0{4})\d{4})|((?!219 09 9999|078 05 1120)(?!666|000|9\d{2})\d{3} (?!00)\d{2} (?!0{4})\d{4})|((?!219099999|078051120)(?!666|000|9\d{2})\d{3}(?!00)\d{2}(?!0{4})\d{4})$
+6

SSN. SSN

  • ,
  • , 9 , -
  • 666,000,900,999,123456789,111111111,222222222,333333333,444444444,555555555,666666666,777777777,888888888,999999999
  • 0000

    ^(?!123([ -]?)45([ -]?)6789)(?!\b(\d)\3+\b)(?!000|666|900|999)[0-9]{3}([ -]?)(?!00)[0-9]{2}\4(?!0000)[0-9]{4}$
    

     ^ - Beginning of string  
    (?!123([ -]?)45([ -]?)6789) - Don't match 123456789, 123-45-6789, 123 45 6789  
    (?!\b(\d)\3+\b) - Don't match 00000000,111111111...999999999. Repeat same with space and dashes. '\3' is for backtracking to (\d)  
    (?!000|666|900|999) - Don't match SSN that begins with 000,666,900 or 999.
    ([ -]?) - Check for space and dash. '?' is used to make space and dash optional. ? is 0 or 1 occurence of previous character.
    (?!00) - the 4th and 5th characters cannot be 00.
    \4 - Backtracking to check for space and dash again after the 5th character. 
    (?!0000) - The last 4 characters cannot be all zeros.
    $ - End of string
    Backtracking is used to repeat a captured group (). Each group is represented sequentially 1,2,3..so on

. https://regex101.com/r/rA2xA2/3

0
source

Source: https://habr.com/ru/post/1622165/


All Articles