var check = checkCodeMelli(4160476164); // return boolean
Last active
August 22, 2017 20:08
-
-
Save ali-master/828d9e4142b3580dbc38ec48d3eacbcd to your computer and use it in GitHub Desktop.
چک کردن درستی کد ملی ایرانی | Check Iranian National Code Validity
// در نسخههای قبل یکسان بودن اعداد نا معتبر تشخیص داده میشد ولی
// اعداد یکسان نامعتبر نیست http://www.fardanews.com/fa/news/127747
/**
* @author Ali Torki (2016)
* @lincense: Public Domain
*/
function checkCodeMelli(codeMelli){
let code = codeMelli.toString() || null;
if (!code.match(/^\d{10}$/)) return false;
code = ('0000' + code).substr(code.length + 4 - 10);
if (parseInt(code.substr(3, 6), 10) === 0) return false;
let lastNumber = parseInt(code.substr(9, 1), 10);
let sum = 0;
for (let i = 0; i < 9; i++){
sum += parseInt(code.substr(i, 1), 10) * (10 - i);
}
sum = sum % 11;
return (sum < 2 && lastNumber === sum) || (sum >= 2 && lastNumber === (11 - sum));
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment