Advertisement
Guest User

Untitled

a guest
Jul 12th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. this.ValidatePersonalNumber = function (input) {
  2.             if(input.indexOf('-') === -1) {
  3.                 if (input.length === 10) {
  4.                     input = input.slice(0, 6) + "-" + input.slice(6);
  5.                 } else {
  6.                     input = input.slice(0, 8) + "-" + input.slice(8);
  7.                 }
  8.             }
  9.             //Error
  10.             if (!input.match(/^(\d{2})(\d{2})(\d{2})\-(\d{4})|(\d{4})(\d{2})(\d{2})\-(\d{4})$/)) {
  11.                 input = "Incorrect";
  12.                 return input;
  13.             };
  14.  
  15.             // Clean input
  16.             input = input.replace('-', '');
  17.             if (input.length === 12) {
  18.                 input = input.substring(2);
  19.             }
  20.  
  21.             // Declare variables        
  22.             var d = new Date(((!!RegExp.$1) ? RegExp.$1 : RegExp.$5), (((!!RegExp.$2) ? RegExp.$2 : RegExp.$6) - 1), ((!!RegExp.$3) ? RegExp.$3 : RegExp.$7)),
  23.                 sum = 0,
  24.                 numdigits = input.length,
  25.                 parity = numdigits % 2,
  26.                 i,
  27.                 digit;
  28.  
  29.             // Check valid date
  30.             if (Object.prototype.toString.call(d) !== "[object Date]" || isNaN(d.getTime())) {
  31.                 return input = "Incorrect";
  32.             }
  33.  
  34.             // Check luhn algorithm
  35.             for (i = 0; i < numdigits; i = i + 1) {
  36.                 digit = parseInt(input.charAt(i), 10);
  37.                 if (i % 2 === parity) { digit *= 2; }
  38.                 if (digit > 9) { digit -= 9; }
  39.                 sum += digit;
  40.             }
  41.  
  42.             if ((sum % 10) !== 0) {
  43.                 input = "Incorrect";
  44.                 return input;
  45.             };
  46.  
  47.             return input;
  48.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement