-
-
Save nekman/1058171 to your computer and use it in GitHub Desktop.
Hello to both of you.
I had a problem that 1976010101 gave a false positive, so I made this edit:
/^(19|20)?(\d{6}(-|\s)\d{4}|(?!19|20)\d{10})$/
(According to wikipedia there is no living swede is born in 1800s.)
@Krillko
Thanks for clarification, yes indeed your code is more smart.
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
Hello
First of all, thanks for this example. It helped me a lot.
But i have a little correction to your code, that maybe you or others can use.
Instead of checking if the first couple of digits are between 6 and 8 digits long, i have altered your regex to only validate SSN numbers which are exactly 6 or 8 digits in length.
Before, it was possible to input an SSN which looked like this -> xxxxxxx-xxxx (7 digits - 4 digits)
Here is my code:
isSwedishSocialSecurityNumber(ssn: string) {
return /^(\d{6}|\d{8})[-|(\s)]{0,1}\d{4}$/.test(ssn);
}
Best regards Jesper
P.S. I am using AngularJS by the way