Skip to content

Instantly share code, notes, and snippets.

@alireza-ahmadi
Created June 8, 2013 07:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alireza-ahmadi/5734422 to your computer and use it in GitHub Desktop.
Save alireza-ahmadi/5734422 to your computer and use it in GitHub Desktop.
/**
* Useful function for Melli code validation
* @author Alireza Ahmadi info@alireza.es
* @param code {String} Melli code
* @return {Boolean} Melli code credential
*/
var MelliCode = function(code){
if(code.length != 10) {
return false;
}
else{
var ctrlCode;
var sum;
var result;
var newControlCode;
code = code.split('');
code.reverse();
code.unshift('+PLACEHOLDER+');
ctrlCode = code[1];
sum = 0;
for(var i=2;i<11;i++){
sum += code[i] * i;
}
result = (sum%11);
newControlCode = (result < 2) ? (result) : (11-result);
return (newControlCode == ctrlCode) ? true : false;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment