Created
June 8, 2013 07:34
-
-
Save alireza-ahmadi/5734422 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Useful function for Melli code validation | |
* @author Alireza Ahmadi [email protected] | |
* @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