Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function SwedishSocialSecurityNumber(socialSecurityNumber){
  2.     this.isOver100 = false;
  3.     this.socialSecurityNumber = socialSecurityNumber;
  4.  
  5.  
  6.     if(!this.hasTheRightPattern())
  7.         throw new Error('The SSN has to have the format YYMMDD-XXXX or YYMMDD+XXXX');
  8.  
  9.     this.year = parseInt(this.socialSecurityNumber.substring(0,2));
  10.     this.month = parseInt(this.socialSecurityNumber.substring(2,4));
  11.     this.day = parseInt(this.socialSecurityNumber.substring(4,6));
  12.     this.code = parseInt(this.socialSecurityNumber.substring(7,11));
  13.    
  14.     if(this.socialSecurityNumber.substring(6, 7) == '+') this.isOver100 = true;
  15.  
  16.     if(!this.isDateValid(this.getFullYear(), this.month, this.day))
  17.         throw new Error('The given date is not valid');
  18.  
  19.     if(this.getCheckSum() != this.socialSecurityNumber.substring(10,11))
  20.         throw new Error("The control number is invalid");
  21.  
  22. }
  23.  
  24.  
  25. SwedishSocialSecurityNumber.prototype.getFullYear = function(){
  26.     currentYear = new Date().getFullYear();
  27.     ret = 0;
  28.     if(this.year < 10){
  29.         ret = currentYear.toString().substring(0,2) + "0" + this.year.toString();
  30.     } else {
  31.         ret = currentYear.toString().substring(0,2) +  this.year.toString();
  32.     }
  33.  
  34.     if(this.isOver100) return parseInt(ret) - 100;
  35.     if( parseInt(ret) > currentYear ) return parseInt(ret) - 100;
  36.  
  37.     return(parseInt(ret));
  38.  
  39. };
  40.  
  41. SwedishSocialSecurityNumber.prototype.getCheckSum = function(){
  42.     variegated = 2;
  43.     temp = 0;
  44.     list = [];
  45.  
  46.     for(i = 0; i < this.socialSecurityNumber.length - 1; i++){
  47.         if( i == 6) continue;
  48.  
  49.         temp = parseInt(this.socialSecurityNumber[i]);
  50.         temp = temp * variegated;
  51.         tempStr = temp.toString();
  52.         for(j = 0; j < tempStr.length; j++) {
  53.             list.push(parseInt(tempStr[j]));
  54.         }
  55.  
  56.         variegated = variegated == 2 ? 1 : 2;
  57.         temp = 0;
  58.     }
  59.  
  60.     sum = 0;
  61.     for(i = 0; i < list.length; i++) sum = sum + list[i];
  62.  
  63.     return(10 - (sum % 10));
  64. };
  65.  
  66. SwedishSocialSecurityNumber.prototype.hasTheRightPattern = function(){
  67.     pattern = /^[0-9]{2}[0-1][0-9][0-3][0-9][-|+][0-9]{4}$/
  68.     return pattern.test(this.socialSecurityNumber);
  69. };
  70.  
  71. SwedishSocialSecurityNumber.prototype.isDateValid = function(){
  72.     d = new Date(this.getFullYear(), this.month - 1, this.day);
  73.     if(isNaN(d)) return false;
  74.     if(d.getFullYear() == this.getFullYear() && (d.getMonth() + 1) == this.month && d.getDate() == this.day) return true;
  75.     return false;
  76. };
  77.  
  78. SwedishSocialSecurityNumber.prototype.isFemale = function() {
  79.     if((parseInt(this.socialSecurityNumber.substring(9, 10)) % 2) === 0) return true;
  80.     return false;
  81. };
  82.  
  83. SwedishSocialSecurityNumber.prototype.isMale = function() {
  84.     return !this.isFemale();
  85. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement