dx_over_dt
dx_over_dt

Reputation: 14318

Enable ASP.Net validator without immediately running validation

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.

Upvotes: 1

Views: 1364

Answers (4)

JeffT
JeffT

Reputation: 201

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

Upvotes: 1

Glia
Glia

Reputation: 381

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

Upvotes: 1

Ian Grainger
Ian Grainger

Reputation: 5526

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

Upvotes: 1

rlb.usa
rlb.usa

Reputation: 15043

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()

Upvotes: 0

Related Questions