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');
@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