Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4542980
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:28:01+00:00 2026-05-21T15:28:01+00:00

My original question I’ve managed to answer by cobbling other answers together from this

  • 0

My original question I’ve managed to answer by cobbling other answers together from this site (thank you StackOverflow!) – just posting here to help others wondering.

I have a live event like:

function addValidator(selector, validator) {
    // Add the selector and validator to a hash map
    $(selector).live('change', validateTag);
}

The table rows are going to vary in number at any time and I don’t want to tightly couple the table creation code with the validation code.

Later another validation function comes along not knowing anything about the first, and I need to avoid that same live selector firing the validateTag function twice, which is going to aggregate all validation messages and present a single UI about a given input.

So – each time someone calls addValidator I need to actually REPLACE the live event handler, or more specifically, expand on its selector (since I’m still calling validateTag, I just need the live handler to cover more ground). I’ll post the answer I came up with below, happy to hear more.

— Update to clarify what I’m doing

I have several pages with dynamic tables that could include any number of rows. I don’t want the validation and the table management code to be tightly coupled. I want to be able to set out inputs in the table rows like:

<input class="required email"/>

And have validation validate it for both the required and email rules onchange(). In code:

Valid.addValidator('input.required', Valid.required);
Valid.addValidator('input.email', Valid.email);

Obviously as rows are added and removed I could keep reapplying the events to the inputs, but that tightly couples the table builder code and the validation code.

Another hack is to just build a single live event like:

$('input, select, textarea').live('change', Valid.validateTag);

And then use a hashmap to look up what exactly applies to this specific tag, but that assumes the only things I’ll ever validate are inputs, selects and textareas, and that the only kind of validation I’ll ever want to do is onchange.

What I really want is to let any kind of validation rule match any tags by selector, without either of the above. The second hack isn’t too bad, but it is a false assumption and is inflexible to custom controls that don’t use one of those 3 basic tags.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T15:28:01+00:00Added an answer on May 21, 2026 at 3:28 pm

    Updated after a lot of tinkering and taking suggestions from the other answers here.

    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);
    }
    

    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.

    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.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question relates to my answer of another of my question. The original question
This is a continuation of this question: Original Question (SO) The answer to this
This question is sort of a follow-up to my original question here . Let's
Moving on from my original question below: Android Sending an email using a list
Here is my original question I've implemented the answer there but still the problem
From the original question (below), I am now offering a bounty for the following:
UPDATED QUESTION WITH ANSWER!! Original Question I've been looking at Eli's object.watch (https://gist.github.com/384583) script
My original question was about whether to keep separate ASPNETDB.MDF from the application database
My original question When I execute the following query in SQLite, I get this
Original question: this bit of javascript code will convert centimeters to feet. But the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.