Reputation: 465
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?
Upvotes: 11
Views: 29402
Reputation: 11
Set the "CausesValidation" to true for the button.
For this kind of issue, do not forget the button type.
You have to set the "CausesValidation" to true, for this to validate without having to write up javascript manually. This is the client side of validation.
You can set validation from the code behind as well but this is enough.
Upvotes: 0
Reputation: 1
I had the same issue. Discovered that you need need to add some framework script references to your script manager. Make sure you have at least these script references withing "script" tags in your script manager.
<asp:ScriptManager ID="ScriptManager1" runat="server" ValidateRequestMode="Enabled" >
<Scripts>
<%--Framework Scripts--%>
<%--<asp:ScriptReference Name="MsAjaxBundle" />--%>
<asp:ScriptReference Name="jquery" />
<asp:ScriptReference Name="jquery.ui.combined" />
<asp:ScriptReference Name="WebForms.js" Path="~/Scripts/WebForms/WebForms.js" />
<asp:ScriptReference Name="WebUIValidation.js" Path="~/Scripts/WebForms/WebUIValidation.js" />
<asp:ScriptReference Name="WebFormsBundle" />
<%--Site Scripts--%>
</Scripts>
</asp:ScriptManager>
You can find those as well in the default master page created by .Net when you create a webforms application.
Upvotes: 0
Reputation: 37205
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.
Upvotes: 4
Reputation: 2119
You may be creating a usability issue here. If you validate mandatory fields on blur, the user will get a lot of "Required Field" errors just because he's tabbing through the fields.
Upvotes: 3
Reputation: 12451
Thats just how the validators work, they don't fire unless there was input to validate or there was a postback. You'll have to do the validation firing yourself if you want it to happen.
I don't know the exact code you'll need but it'll have to do with handling onblur and overriding evaluation function:
function blurred(sender) {
var validator = sender.Validators[0]
validator.evaluationfunction(validator);
}
and on the textbox:
<asp:TextBox runat="server" ID="txt" onBlur="blurred(this)"></asp:TextBox>
Upvotes: 0
Reputation: 4537
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();
}
Upvotes: 4
Reputation: 37205
A form can contain several validation groups. AFAIK validation is only triggered through a post-back, activating the validators of the corresponding validator group. Only after the post-back does the validator add its client-side Javascript validation code.
Upvotes: 0
Reputation: 3457
did you set the EnableClientScript attribute/property to true? do you have a default value for the text box? if so you need to set the InitialValue property to that default value
Upvotes: 2