1

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.

4 Answers 4

1

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a reason you need getJqueryObjectFromArrayOfEls? Couldn't you just use $(Page_Validators)?
Oh. Can I? Wow. That'd be nicer... I'll try to remember to test and simplify the answer (assuming it works) thanks @dfoverdx!
1

I just came across a similar requirement. WhatI did was first to set a valid value to the compnent to be validated, then enable the validator and finally I cleared the value in the component again. Something like

$('#<%= tbxValue.ClientID %>').val('X'); // set a value to the textbox
ValidatorEnable(<%= rfvValue.ClientID %>, true); // enable the required field validator
$('#<%= tbxValue.ClientID %>').val(''); // clear the value from the textbox

Comments

1

Instead of using that function, you can make your own function that has some of the same code as ValidatorEnable, but wont trigger the validation.

function myValidatorEnable(val, enable) {
        val.enabled = (enable != false);
    }

1 Comment

This is the correct answer as the ValidatorEnable function calls the following: ValidatorEnable(val, enable) { val.enabled = (enable != false); ValidatorValidate(val); ValidatorUpdateIsValid(); } (just go to your page's javascript console and type in ValidatorEnable and press enter to see the function signature) So just calling val.enabled = (enabled != false); will do what you need without running the validation
0

Yes, it's possible in a variety of ways. The two most common and perhaps sensible things to do are :

  • Have validation occur completely in the code-behind via code only
  • Have ASP.NET validators that are set to enabled=false and then turn them on and call Page.Validate()

2 Comments

Is enabled=false in the .ascx file or in javascript somewhere? Won't turning them on call Page.Validate() immediately? Running it only in code-behind means that they won't get the error message as soon as they specify an invalid empty value.
@dfoverdx When you turn on validators in the code behind, it doesn't call Page.Validate immediately, it is dependent on what the user does next. Also remember that you can set enabled= conditionally . enabled='<%# Eval("MyControl").Visible %>'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.