I'm having an issue with a standard ASP.NET page that has a TextBox and a RequiredFieldValidator. The steps to reproduce are quite simple:
The RequiredFieldValidator works fine in both cases after a postback, however it seems the client-side code isn't firing until something is entered into the textbox (and then deleted).
Does anyone have a solution to this without hacking away at JavaScript myself?
Is it possible this behavior is by design to suppress the appearance of validation controls until user input?
Generally speaking, Validate() gets called whenever a control is clicked that has CausesValidation set to true, like a submit button.
In any case, a poor mans work around, you could call the page Validate() function from the Load event handler. This will make things clearer to tab happy users that they need to enter something in. E.g.
protected void Page_Load(object sender, EventArgs e)
{
Validate();
}
A follow-up to my previous answer:
Validation occurs in the onchange event, rather than the onblur. onchange fires when the focus is lost AND the control value has changed.
To trigger validation in the onblur event, I added the following code in the Page_Load():
ScriptManager.RegisterStartupScript(this, GetType(), "js" + myTextBox.ClientID,
"ValidatorHookupEvent(document.getElementById(\"" + myTextBox.ClientID +
"\"), \"onblur\", \"ValidatorOnChange(event);\");", true);
Works in ASP.Net 2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With