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.com/ijetex/5/edit
What about this?
Full example changing the error message