Skip to content

Instantly share code, notes, and snippets.

@mhf-ir
Last active September 1, 2020 03:26
Show Gist options
  • Save mhf-ir/3b6d67e73f04874eea6baece3e43a5c0 to your computer and use it in GitHub Desktop.
Save mhf-ir/3b6d67e73f04874eea6baece3e43a5c0 to your computer and use it in GitHub Desktop.
اعتبار سنجی کد ملی ایرانی - Iranian identity card validation
/**
* 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