Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- this.ValidatePersonalNumber = function (input) {
- if(input.indexOf('-') === -1) {
- if (input.length === 10) {
- input = input.slice(0, 6) + "-" + input.slice(6);
- } else {
- input = input.slice(0, 8) + "-" + input.slice(8);
- }
- }
- //Error
- if (!input.match(/^(\d{2})(\d{2})(\d{2})\-(\d{4})|(\d{4})(\d{2})(\d{2})\-(\d{4})$/)) {
- input = "Incorrect";
- return input;
- };
- // Clean input
- input = input.replace('-', '');
- if (input.length === 12) {
- input = input.substring(2);
- }
- // Declare variables
- var d = new Date(((!!RegExp.$1) ? RegExp.$1 : RegExp.$5), (((!!RegExp.$2) ? RegExp.$2 : RegExp.$6) - 1), ((!!RegExp.$3) ? RegExp.$3 : RegExp.$7)),
- sum = 0,
- numdigits = input.length,
- parity = numdigits % 2,
- i,
- digit;
- // Check valid date
- if (Object.prototype.toString.call(d) !== "[object Date]" || isNaN(d.getTime())) {
- return input = "Incorrect";
- }
- // Check luhn algorithm
- for (i = 0; i < numdigits; i = i + 1) {
- digit = parseInt(input.charAt(i), 10);
- if (i % 2 === parity) { digit *= 2; }
- if (digit > 9) { digit -= 9; }
- sum += digit;
- }
- if ((sum % 10) !== 0) {
- input = "Incorrect";
- return input;
- };
- return input;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement