Form Validation using jQuery Validation plugin


image

In the example above, we only used three validation methods (required, email and url). There are several other methods that can be used here. Here are a few of them:

  • remote: requests a resource to check the element for validity.
  • min: makes the element require a given minimum.
  • date: makes the element require a date.
  • creditcard: makes the element require a credit card number.
  • equalTo: requires the element to be the same as another one.

You can find an exhaustive list of built-in validation methods at http://docs.jquery.com/Plugins/Validation#List_of_built-in_Validation_methods.

But I can’t find a suitable built-in method for my situation. Can I write my own method?” Yes you can! And its really easy.

Writing Your Own Validation Method

Calling the jQuery.validator.addMethod() method. It takes three parameters:

  • name: The name of the method, used to identify and referencing it, must be a valid javascript identifier.
  • method: the actual method implementation, returning true if an element is valid.
  • message: The default message to display for this method.

In the validate function, we specify that the ‘sport’ field should be validated using the selectNone method.

image

Make sure to look at the source code behind these demos

Via Form Validation using jQuery

Another tutorial here

Leave a comment