Copying Jquery Validation from one element to another

A simple note to self about copying jquery validations on one element to another.

1. First get the validation rules pertaining to the element you want to copy from using the validation jquery extensions

var rules = $(‘#elementtocopy’).rules();

2. The get the messages that match those rules – these are in the settings object for the element. Note how element name is used, and not element id.

rules[‘messages’] = $(‘form’).data(‘validator’).settings[“your element name, NOT id”];

3. Simply invoke the validation jquery extension on the selector that you wish to apply the rules to

$(‘#newelementid’).rules(“add”, args);

Simple eh 😉

Advertisements
This entry was posted in jQuery. Bookmark the permalink.

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