Add a custom method in jQuery validation plugin


jQuery validate plugin has rich set of method to validate html form. To add custom method of validation in this plugin see following example which shows add domain validation method to html input box.

jQuery(document).ready(function($) {
    jQuery.validator.addMethod("domain", function(value, element) {
        return this.optional(element) || /^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)example.com/.test(value);
    }, "The URL must start with https://example.com/");
});
<input type="text" size="15" value="" id="domain" name="domain" class="domain">

Visit this link for additional information : http://jqueryvalidation.org/files/demo/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s