-
-
Save nekman/1058171 to your computer and use it in GitHub Desktop.
I have another improvement to add:
/^(19|20)?(\d{6}([-+]|\s)\d{4}|(?!19|20)\d{10})$/
I added the possibility to use a +
instead of -
because this can be used for numbers older than 100 years.
Thanks for your previous versions!
I have another improvement to add:
/^(19|20)?(\d{6}([-+]|\s)\d{4}|(?!19|20)\d{10})$/
I added the possibility to use a
+
instead of-
because this can be used for numbers older than 100 years.
Somebody was on Wikipedia before googling :D good job!
I have totally missed all these comments! Fun that an old gist from 2011 (!) was used by someone else than me :)
Don't know if the OP or anyone will read this, but this is my version, and it seems to work!
^(19|20)?\d{2}(01|02|03|04|05|06|07|08|09|10|11|12)((0[1-9])|(1|2)[0-9]|(30|31))-\d{4}$
The problem with the expressions so far, is that they don't validate the date, after the year part. So an SSN like 852240-1010 gives a match, but we all know, nobody can be born in the 22th month, and on the 40th day of the year.
I'm studying to become a web developer in C# .NET, and our assignment was to create an app to calculate the age of a person with a Swedish social security number. I wanted to validate the SSN, so that DateTime objects wouldn't throw an exception.
You can see my solution in my repositories, where I use the above regular expression. I know, it is overly complicated, but I haven't found a way to simplify it, wish I would!
Watch out with the regex above me! It's true that "nobody can be born in the 22th month" but in some edge cases skatteverket uses numbers with these invalid dates
@Krillko
Thanks for clarification, yes indeed your code is more smart.