javascript - Custom Validation for jQuery Validate (two method in one) - Stack Overflow

I would like to know, how can I call another custom validation method of jquery validate in my method t

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 badges
Add a ment  | 

3 Answers 3

Reset to default 4

What 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);
//..
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744814009a4595218.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信