Skip to content

Instantly share code, notes, and snippets.

@slavama
Created September 26, 2019 11:26
Show Gist options
  • Save slavama/9d148daf5cda66830e0da8b76bcaa023 to your computer and use it in GitHub Desktop.
Save slavama/9d148daf5cda66830e0da8b76bcaa023 to your computer and use it in GitHub Desktop.
Методы валидации ИНН и СНИЛС для jquery.validate.js
$.validator.addMethod("inn", function (value, element) {
var multipliers = [3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8],
inn = value.split(''), i, j, ch = [0, 0, 0];
for (i = 0; i < 12; i++)
for (j = 0; j < 3; j++)
if (multipliers[i + j])
ch[j] = ch[j] + inn[i] * multipliers[i + j];
if (inn.length == 10)
return inn[9] == ch[2] % 11 % 10;
else if (inn.length == 12)
return inn[10] == ch[1] % 11 % 10 && inn[11] == ch[0] % 11 % 10;
else
return this.optional(element) || !value;
}, "Ошибка при наборе ИНН");
$.validator.addMethod("snils", function (value, element) {
var result = false;
var sum = 0;
var snils = value.replace(/\D/g,'');
for (var i = 0; i < 9; i++) {
sum += parseInt(snils[i]) * (9 - i);
}
var checkDigit = 0;
if (sum < 100) {
checkDigit = sum;
} else if (sum > 101) {
checkDigit = parseInt(sum % 101);
if (checkDigit === 100) {
checkDigit = 0;
}
}
if (checkDigit === parseInt(snils.slice(-2))) {
result = true;
}
return this.optional(element) || result;
}, "Ошибка при наборе СНИЛС");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment