Question

I've been scouring SO here to find an answer, but am coming up short...

I have this addMethod for the jquery Validator script:

$.validator.addMethod("emailz", function(value, element) {
        return this.optional(element) || /^[a-zA-Z0-9._-]+@gmail.com$/i.test(value);
    }, "Please enter a valid Email. ie... mrbrown@gmail.com");

I've added it to the:

$('#id').validate({
  rules: {
   emailz:{required:true},
   otherrulesworking:{},
   ect:{}
  }
})

Other rules are working that are part of the additional .js file. Also, emailz is a unique ID.

I get no error and the required doesn't work from the rules. Why isn't the rule working?

using jquery validation 1.11.x

Was it helpful?

Solution

  • You need to put all of your rules inside of the rules option. edit: OP updated

  • You need to assign the rule to a field only by the field's name attribute.

  • You seem to be assigning the required rule to the emailz method/rule. You cannot assign a rule to a rule. Your emailz rule can only be declared by a boolean true.


$('#myform').validate({      // <- initialize the plugin on form with id="myform"
    rules: {                 // <- option
        somefield: {         // <- field name
            emailz: true,    // <- a rule/method declared
            required: true   // <- another rule/method declared 
        },
        anotherfield: {
            required: true
        }
    }
})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top