Skip to content

Instantly share code, notes, and snippets.

@nekman
Created July 1, 2011 09:33
Show Gist options
  • Save nekman/1058171 to your computer and use it in GitHub Desktop.
Save nekman/1058171 to your computer and use it in GitHub Desktop.
Simple JS regexp for swedish social security number
/*NOTE:
This is just a simple pattern check, so: /^\d{6,8}[-|(\s)]{0,1}\d{4}$/.test("000000 0000") // true
/^\d{6,8}[-|(\s)]{0,1}\d{4}$/.test('195505055555'); // true
/^\d{6,8}[-|(\s)]{0,1}\d{4}$/.test('19550505-5555'); // true
/^\d{6,8}[-|(\s)]{0,1}\d{4}$/.test('550505-5555'); // true
/^\d{6,8}[-|(\s)]{0,1}\d{4}$/.test('550505 5555'); // true
/^\d{6,8}[-|(\s)]{0,1}\d{4}$/.test('5505055555'); // true
*/
//JS
var isSwedishSocialSecurityNumber = function(str) {
return /^\d{6,8}[-|(\s)]{0,1}\d{4}$/.test(str);
};
//Dojo
(function($) {
$.mixin($, {
isSwedishSocialSecurityNumber : function(str) {
return /^\d{6,8}[-|(\s)]{0,1}\d{4}$/.test(str);
}
});
})(dojo);
//dojo.isSwedishSocialSecurityNumber('195505055555');
//jQuery
(function($) {
$.extend({
isSwedishSocialSecurityNumber : function(str) {
return /^\d{6,8}[-|(\s)]{0,1}\d{4}$/.test(str);
}
});
})(jQuery);
//jQuery.isSwedishSocialSecurityNumber('195505055555');
@Evilj84
Copy link

Evilj84 commented Jul 29, 2015

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

@Krillko
Copy link

Krillko commented Jul 8, 2016

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.)

@tahiralvi
Copy link

@Krillko
Thanks for clarification, yes indeed your code is more smart.

@manmal
Copy link

manmal commented Jun 15, 2018

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!

@ShadOoW
Copy link

ShadOoW commented Aug 24, 2019

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!

@nekman
Copy link
Author

nekman commented Aug 24, 2019

I have totally missed all these comments! Fun that an old gist from 2011 (!) was used by someone else than me :)

@Pianotehead
Copy link

Pianotehead commented Sep 12, 2020

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!

@boltgolt
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment