Skip to content

Instantly share code, notes, and snippets.

@kandros
Created September 10, 2015 14:00
Show Gist options
  • Save kandros/d3ecae6169fd2321e9e3 to your computer and use it in GitHub Desktop.
Save kandros/d3ecae6169fd2321e9e3 to your computer and use it in GitHub Desktop.
function validateID (id) {
//"If ID Document Type is 'Identity Card' then Document Number should be in format XXXDDDDDD (three letters and six digits). To verify the correctness letters
//should be changed to numbers using follwing table (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
//10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35). Each from received 9 numbers should be multiplied by corresponding
//number from the following list (7, 3, 1, 9, 7, 3, 1, 7, 3). After adding results division by 10 should be equal to 0."
if (id.length === 0) {
return false;
}
var i = 0;
var type = 'Identity Card';
var result = /^[A-Z]{3}\d{6}/.test(id);
if (!result) {
return false;
}
var w = [7, 3, 1, 9, 7, 3, 1, 7, 3];
var numid =[];
for(i=0;i<3;i++) {
var temp = id.charCodeAt(i)-55;
numid.push(temp);
}
for ( i = 3; i<id.length;i++)
numid.push(parseInt(id.charAt(i)));
var sum = 0;
for ( i = 0; i<numid.length;i++)
sum += numid[i]*w[i];
var m = sum % 10;
if (m === 0) {
console.log("Valid!");
return true;
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment