Skip to content

Instantly share code, notes, and snippets.

@miladkdz
Created February 17, 2015 08:58
Show Gist options
  • Save miladkdz/8bc706e08b851ee161e9 to your computer and use it in GitHub Desktop.
Save miladkdz/8bc706e08b851ee161e9 to your computer and use it in GitHub Desktop.
Iranian National Code Validation in jQuery
function checkNC(code) {
var L = code.length;
if (L < 8 || parseInt(code, 10) == 0) return false;
code = ('0000' + code).substr(L + 4 - 10);
if (parseInt(code.substr(3, 6), 10) == 0) return false;
var c = parseInt(code.substr(9, 1), 10);
var s = 0;
for (var i = 0; i < 9; i++)
s += parseInt(code.substr(i, 1), 10) * (10 - i);
s = s % 11;
return (s < 2 && c == s) || (s >= 2 && c == (11 - s));
}
jQuery.validator.addMethod("checkNationalCode", function(value, element) {
return this.optional(element) || checkNC(value);
}, "لطفاً کد ملی صحیحی وارد کنید");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment