jQuery Validate() addMethod forces "required:true"
So i got a form with the jQuery validate() plugin. I added a custom method to check if the CNPJ (id number for companies in Brazil) is valid or not. The field is not required, i just 开发者_运维问答wish to check its value if someone's writting something. The problem is that when adding this rule:
rules: {
cnpjcampo: {cnpjvalidate: true}
}
It does make it "required".
I've tryed this:
rules: {
cnpjcampo: {required:false, cnpjvalidate: true}
}
Does not affect anything.
My form is using the jQuery wizard form as well as chosen and the [Form plugin].
To get a clear view of what i'm doing, i've set up a jsFiddle. I you hit the Next button (Proximo) it'll trigger the validate plugin and tell you that the name (nome fantasia) is wrong, which is correct as set to "required" and also the CNPJ, which should not.
Found a solution. I erased the addMethod i was using, replaced it with another js file found here and changed the rules to this:
validationOptions: {
rules: {
"cpfsocio1" : { cpf: 'valid' /* 'valid' : sem formatação; 'format' : só formato; 'both' : os 2 */ },
"cpfsocio2" : { cpf: 'valid' },
"cnpjcampo" : { cnpj: 'valid' }
}
},
Here's the JsFiddle updated for those interested.
精彩评论