Hi, may be added in a validation field Jquery, for example, the document often using in Brazil, called the CPF. I need to check if it is valid, with the code below:
jQuery.validator.addMethod("cnpj", function (cnpj, element) {
cnpj = jQuery.trim(cnpj);
jQuery.validator.addMethod("cpf", function (value, element) {
value = jQuery.trim(value);
value = value.replace('.', '');
value = value.replace('.', '');
cpf = value.replace('-', '');
while (cpf.length < 11) cpf = "0" + cpf;
var expReg = /^0+$|^1+$|^2+$|^3+$|^4+$|^5+$|^6+$|^7+$|^8+$|^9+$/;
var a = [];
var b = new Number;
var c = 11;
for (i = 0; i < 11; i++) {
a[i] = cpf.charAt(i);
if (i < 9) b += (a[i] * --c);
}
if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11 - x }
b = 0;
c = 11;
for (y = 0; y < 10; y++) b += (a[y] * c--);
if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11 - x; }
if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10]) || cpf.match(expReg)) return this.optional(element) || false;
return this.optional(element) || true;
}, "Informe um CPF válido."); // Mensagem padrão
Viewing 1 reply thread
The support post ‘Brazilian Doc (CPF Validate)’ is closed to new replies.
Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.
If you would like help from the Event Espresso staff, then please purchase a support license right now so you can create a support topic in our premium support forums.