Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function SwedishSocialSecurityNumber(socialSecurityNumber){
- this.isOver100 = false;
- this.socialSecurityNumber = socialSecurityNumber;
- if(!this.hasTheRightPattern())
- throw new Error('The SSN has to have the format YYMMDD-XXXX or YYMMDD+XXXX');
- this.year = parseInt(this.socialSecurityNumber.substring(0,2));
- this.month = parseInt(this.socialSecurityNumber.substring(2,4));
- this.day = parseInt(this.socialSecurityNumber.substring(4,6));
- this.code = parseInt(this.socialSecurityNumber.substring(7,11));
- if(this.socialSecurityNumber.substring(6, 7) == '+') this.isOver100 = true;
- if(!this.isDateValid(this.getFullYear(), this.month, this.day))
- throw new Error('The given date is not valid');
- if(this.getCheckSum() != this.socialSecurityNumber.substring(10,11))
- throw new Error("The control number is invalid");
- }
- SwedishSocialSecurityNumber.prototype.getFullYear = function(){
- currentYear = new Date().getFullYear();
- ret = 0;
- if(this.year < 10){
- ret = currentYear.toString().substring(0,2) + "0" + this.year.toString();
- } else {
- ret = currentYear.toString().substring(0,2) + this.year.toString();
- }
- if(this.isOver100) return parseInt(ret) - 100;
- if( parseInt(ret) > currentYear ) return parseInt(ret) - 100;
- return(parseInt(ret));
- };
- SwedishSocialSecurityNumber.prototype.getCheckSum = function(){
- variegated = 2;
- temp = 0;
- list = [];
- for(i = 0; i < this.socialSecurityNumber.length - 1; i++){
- if( i == 6) continue;
- temp = parseInt(this.socialSecurityNumber[i]);
- temp = temp * variegated;
- tempStr = temp.toString();
- for(j = 0; j < tempStr.length; j++) {
- list.push(parseInt(tempStr[j]));
- }
- variegated = variegated == 2 ? 1 : 2;
- temp = 0;
- }
- sum = 0;
- for(i = 0; i < list.length; i++) sum = sum + list[i];
- return(10 - (sum % 10));
- };
- SwedishSocialSecurityNumber.prototype.hasTheRightPattern = function(){
- pattern = /^[0-9]{2}[0-1][0-9][0-3][0-9][-|+][0-9]{4}$/
- return pattern.test(this.socialSecurityNumber);
- };
- SwedishSocialSecurityNumber.prototype.isDateValid = function(){
- d = new Date(this.getFullYear(), this.month - 1, this.day);
- if(isNaN(d)) return false;
- if(d.getFullYear() == this.getFullYear() && (d.getMonth() + 1) == this.month && d.getDate() == this.day) return true;
- return false;
- };
- SwedishSocialSecurityNumber.prototype.isFemale = function() {
- if((parseInt(this.socialSecurityNumber.substring(9, 10)) % 2) === 0) return true;
- return false;
- };
- SwedishSocialSecurityNumber.prototype.isMale = function() {
- return !this.isFemale();
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement