Form Validation - defining a custom rule
Here we define a new validation rule called "netid" (three characters and three numbers, e.g. 'abc123')
- jQuery.validator.addMethod(name, method [,message])
- Regular expression var pattern = /^[a-zA-Z]{3}[0-9]{3,4}$/;
 this is a regular expression pattern (regex) that matches three characters (e.g. abc) followed by three numbers (e.g. 123).- "^" matches the beginning of the string
- "[a-zA-Z]" defines a character class of all lower and upper alphabetic characters
- "{3}" indicates that the character class preceding occurs three times
- "[0-9]" defines a character class of numbers - e.g. 0,1,2,3,4,5,6,7,8,9
- "{3,4}" indicates that the character class preceding occurs at least three times and at most four times
- "$" matches the end of the string
- pattern.test(value)returns- trueor- false, depending on whether the 'value' matches the pattern
 
$(document).ready( function() {
    /* addMethod for netid validation */
    $.validator.addMethod(
      "netid",
       function(value, element) {
          var pattern = /^[a-zA-Z]{3}[0-9]{3,4}$/;
          return this.optional( element ) || pattern.test( value );
       },
       'Please enter a valid NetID (e.g. abc123).'
     );
    var validation = $("#myForm").validate(
         {
           rules: {
             name: { required: true, minlength: 2 },
             email: { required: true, email: true },
             netid: { required: true, netid: true }
          }
    );
});