[SOLVED] Enable ASP.Net validator without immediately running validation

Issue

Question

Can I enable an ASP.Net validator without running validation immediately (i.e. giving the user a chance to input values before running it, but still force running validation on form submit)?

Background

I have a form that allows a person to input their family data when they check in their child(ren) into a public daycare system. We have five fields that I need to force parents to actively consider (allergies, special needs, etc). The values are not actually required, so too many parents were just skipping over the fields when their children should have had values specified.

My solution is to have a required field validator that’s disabled if they click an N/A checkbox next to the textbox. (If someone has a better solution, I’m all ears; the UI of this form–which I inherited–makes me want to gouge out my eyes.)

The other thing is that when the "Add Family" button is clicked, four empty rows are auto-generated: two adults and two children. Whether a row represents a child or an adult is determined by a drop-down in that row. If "adult" is selected, or if the first name textbox in that row is empty, the validators are disabled.

The validators of all five fields are enabled as soon as both the drop-down selected value is "child" AND the first name textbox is not empty. The issue is that running ValidatorEnable() causes the form to validate. In the common case, the fields will be empty, since first name and "family role" (adult/child) will be inputted before whether that person has allergies or are potty trained. This means as soon as they input the name into a row specified as child, ASP.Net’s all like "HEY, DUMMY, YOU HAVE AN INVALID FIELD!!!1!".

So, I would like to enable the validator, but prevent validation until the user actually either inputs an empty value into the allergy textbox, or they try to submit the form.

Is this possible?

Note, I would use a custom field validator, but validation isn’t run on empty fields; afaik, only required field validators do that.

Solution

I’ve just adapted the answer here: http://veskokolev.blogspot.co.uk/2007/11/how-to-disable-validation-group-with.html to enable some validators client-side with jQuery. Once they were enabled, the validation function did run – I don’t know how to stop it (without modifying the ASP.NET JS code, which I guess is an option?); so I simply hid the messages until the validation is run again (which is easy enough with $(x).hide()).

Considering all the client-side code does is show the warning and stop you submitting I think this is OK. The usual things that will cause the validator to re-run will re-show the message.

So if I have validators like:

<asp:Validator runat="server" CssClass="js-validator-set-a" />

I can use this Javascript:

toggleValidators('.js-validator-set-a', true);    

function toggleValidators(selector, enable) {
    // get the page validators into a jQuery object
    var vals = getJqueryObjectFromArrayOfEls(Page_Validators)

    vals.filter(selector).each(function (i, o) {
        ValidatorEnable(o, enable);
        $(o).hide();
    });
};

function getJqueryObjectFromArrayOfEls(elsArray) {
    var x = $();
    $.each(Page_Validators, function (i, o) {
        x = x.add($(o));
    });
    return x;
}

Answered By – Ian Grainger

Answer Checked By – Terry (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *