1

I am aware of the following :-

$( function() 
{
    $( "#register" ).validate
    ({
        rules: 
        {
            ...
            email: {
                    required: true,
                    email: true
                },
            ...

But what does email: true actually mean. Which characters are allowed and which are aren't ? Is there any reference online that will allow me to find the exact meaning ? Thanks.

Grateful
  • 9,685
  • 10
  • 45
  • 77

1 Answers1

1

jQuery validation plugin for email

Rules (default: rules are read from markup (classes, attributes, data))

Key/value pairs defining custom rules. Key is the name of an element (or a group of checkboxes/radio buttons), value is an object consisting of rule/parameter pairs or a plain String. Can be combined with class/attribute/data rules.

Each rule can be specified as having a depends-property to apply the rule only in certain conditions. See the second example below for details.

Example: Specifies a name element as required and an email element as required (using the shortcut for a single rule) and a valid email address (using another object literal).

$(".selector").validate({
  rules: {
    // simple rule, converted to {required:true}
    name: "required",
    // compound rule
    email: {
      required: true,
      email: true
    }
  }
});

Above is the theory. We want to find what exactly means using this tag.So, let's see the file core.js of the plugin which defines the regex we want to learn. The answer to this question is the regex the plugin is using.

Check this regex here : http://regexr.com/39j49

// http://jqueryvalidation.org/email-method/
email: function( value, element ) {
 ...
return this.optional( element ) || /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test( value );

},

In addition, you can add generally a custom rule by following this doc. Something like that:

jQuery.validator.addMethod("myemailrule", function(value, element) {
  return this.optional(element) /* || your regex here*/
},

$(".selector").validate({
  rules: {
    // simple rule, converted to {required:true}
    name: "required",
    // added custom rule
    email: {
      required: true,
      myemailrule: true
    }
  }
});

Hope, it helps!

Giannis Grivas
  • 3,374
  • 1
  • 18
  • 38