I would like to know, how can I call another custom validation method of jquery validate in my method to validate some data. For sample, I have a field called 'Document ID' that can accept CPF or CNPJ (both are brazilians documents), and I need to validate it with jquery validate. I'm doing something like this:
jQuery.validator.addMethod("cpf", function(value, element) {
// my validation for CPF Documents
}, "CPF inválido!");
jQuery.validator.addMethod("cnpj", function(value, element) {
// my validation for CNPJ Documents
}, "CNPJ inválido!");
Both works fine when I have one field for each type but I have only one and I have to discovery what my user type on textfield and validate it, somethind like this:
jQuery.validator.addMethod("documentoId", function(value, element) {
if (value.lenght <= 11) {
// validation for CPF and change the message
}
else if (value.lenght <= 14) {
// validation for CNPJ and change the message
}
}, 'Documento inválido');
but I don't know how to call these custom functions (cpf and cnpj) inside my another function (documentId).
How can I do it? and how can I change the message inside my custom validation ?
Thank you! Cheers!
I've posted my code in jsbin, if you want to look:
I would like to know, how can I call another custom validation method of jquery validate in my method to validate some data. For sample, I have a field called 'Document ID' that can accept CPF or CNPJ (both are brazilians documents), and I need to validate it with jquery validate. I'm doing something like this:
jQuery.validator.addMethod("cpf", function(value, element) {
// my validation for CPF Documents
}, "CPF inválido!");
jQuery.validator.addMethod("cnpj", function(value, element) {
// my validation for CNPJ Documents
}, "CNPJ inválido!");
Both works fine when I have one field for each type but I have only one and I have to discovery what my user type on textfield and validate it, somethind like this:
jQuery.validator.addMethod("documentoId", function(value, element) {
if (value.lenght <= 11) {
// validation for CPF and change the message
}
else if (value.lenght <= 14) {
// validation for CNPJ and change the message
}
}, 'Documento inválido');
but I don't know how to call these custom functions (cpf and cnpj) inside my another function (documentId).
How can I do it? and how can I change the message inside my custom validation ?
Thank you! Cheers!
I've posted my code in jsbin, if you want to look: http://jsbin./ijetex/5/edit
Share asked Sep 12, 2011 at 19:22 Felipe OrianiFelipe Oriani 38.6k19 gold badges138 silver badges201 bronze badges3 Answers
Reset to default 4What about this?
jQuery.validator.addMethod("documentoId", function(value, element) {
if (value.length <= 11) {
jQuery.validator.methods.cpf.call(this, value, element);
}
else if (value.length <= 14) {
jQuery.validator.methods.cnpj.call(this, value, element);
}
}, 'Documento inválido');
Full example changing the error message
jQuery.validator.addMethod("documento", function(value, element) {
// remove pontuações
value = value.replace('.','');
value = value.replace('.','');
value = value.replace('-','');
value = value.replace('/','');
if (value.length <= 11) {
if(jQuery.validator.methods.cpf.call(this, value, element)){
return true;
} else {
this.settings.messages.documento.documento = "Informe um CPF válido.";
}
}
else if (value.length <= 14) {
if(jQuery.validator.methods.cnpj.call(this, value, element)){
return true;
} else {
this.settings.messages.documento.documento = "Informe um CNPJ válido.";
}
}
return false;
}, "Informe um documento válido.");
jQuery.validator.addMethod("cnpj", function(value, element) {
return this.optional(element) || /^[0-9]{2}.[0-9]{3}.[0-9]{3}?\/[0-9]{4}-[0-9]{2}$/.test(value);
}, "Formato: 00.000.000/0000-00");
how about creating those function outside of the scope of the validation definition (i.e not using anonymous functions)?
function cpfValide(value,element) {
//something
}
function cnpjValide(value,element){
//something
}
jQuery.validator.addMethod("cpf", cpfValide, "CPF inválido!");
jQuery.validator.addMethod("documentoId", function(value, element) {
return cpfValidate(value,element) || cnpjValidate(value,element);
//..
}