Last active
September 1, 2020 03:26
-
-
Save mhf-ir/3b6d67e73f04874eea6baece3e43a5c0 to your computer and use it in GitHub Desktop.
اعتبار سنجی کد ملی ایرانی - Iranian identity card validation
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
/** | |
* Iranian identity card validation | |
* اعتبار سنجی کد ملی ایرانی | |
* | |
* @param {string|integer} value | |
* @returns {boolean} | |
*/ | |
const iranianIdentityCardValidation = (value) => { | |
if (typeof value === 'undefined' || !value) { | |
return false; | |
} | |
const check = parseInt(value[9], 10); | |
let sum = 0; | |
for (let i = 0; i < 9; i += 1) { | |
sum += parseInt(value[i], 10) * (10 - i); | |
} | |
sum %= 11; | |
const result = (sum < 2 && check === sum) || (sum >= 2 && check + sum === 11); | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment