Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Updated after a lot of tinkering and taking suggestions from the other answers here.</p> <pre><code>addRule: function(selector, validator) { // Save the rule Valid.rules.push({selector: selector, validator: validator}); // Hunt for an existing live selector from prev addRule calls and expand on it if need be var existingLiveEvents = $(document).data('events'); if (existingLiveEvents.change) { var existing = existingLiveEvents.change.find(function (item) { return item.origHandler == Valid.validateThis; }); if (existing) { // Expand the selector to include past rules selector = existing.selector + ', ' + selector; // And kill off the old validate selector $(existing.selector).die('change', Valid.validateThis); } } $(selector).live('change', Valid.validateThis); } </code></pre> <p>validateThis() uses the jQuery .is() method to check each rule for whether this tag is affected by that rule, and if so runs the validator on it.</p> <p>If you look carefully, there's also a .find() call in there from an array library I made - it does what you'd expect: Iterate an array until a comparator function you pass in returns true. Then it hands you the item it stopped on, or null.</p> <p>If people are interested in this library I'm happy to consider going open source with it. The jQuery validation plugin is interesting, but it doesn't seem to handle the dynamic sets of input I'm using. There's also some other magic in my library that gets the label for an input based on a ruleset.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload