I’m stuck with jQueries validation plugin
My input give me always an error. Code can you find here: http://jsfiddle.net/OrangeTux/6zUMd/1/
I want to create some custom rules, like postalcode, phonenumber and some other specific numbers. Therefore, I create a new method for the regular expression rule. This rule works correctly.
$.validator.addMethod("regexp", function(value, element, param) {
return this.optional(element) || !param.test(value);
});
After that I create a new method:
jQuery.validator.addMethod('postalCode', function(value, element) {});
Then I use create a new class and add the rules
//add validation rules for each custom method
jQuery.validator.addClassRules({
postalCode: {
'postalCode' : true,
//regexp: /[^[0-9]{10}$]/, //<---- This regex doesn't work
//email: true // <----- This rule doens't work either
}
})
How can I solve this?
You’ve got a few things a bit backwards. First of all, the
regexrule is not quite right. You want to returntruefrom the method when the rule passes, so you should return:Beyond that though, the preferred way for adding a custom rule like this is to use
addMethod, and thenaddClassRulesto add a class rule that includes that method:Example: http://jsfiddle.net/LCA9C/