Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make requiredfieldvalidator error message display after click submit button

now, the error message will display if I move out of current textbox. I don't want to display it until I click submit button.

like image 795
Tony Avatar asked May 18 '10 06:05

Tony


3 Answers

This isn't possible when ClientScript is enabled for your validators. And the ClientScript is by default enabled for your validators. You need to disable this by settings EnableClientScript to False in your source.

Now in the event handler of your submit button call Page.Validate() and Page.IsValid to see if every validators did pass the test.

Example:

<asp:RequiredFieldValidator ID="rfvFirstName" runat="server" ControlToValidate="txtFirstName" EnableClientScript="false" Display="Dynamic" SetFocusOnError="true" />

Page.Validate();
if (!Page.IsValid)
{
     //show a message or throw an exception
}
like image 53
Joop Avatar answered Sep 24 '22 07:09

Joop


Use a validation summary control somewhere on your page...

<asp:validationsummary id="valSummary" runat="server" headertext="Validation Errors:" cssclass="ValidationSummary" />` 

Then to validate:

<asp:textbox id="txtPostalCode" runat="server" MaxLength="250" Width="160px" text='<%# Bind("PostalCode") %>'></asp:textbox>

<asp:requiredfieldvalidator id="reqPostalCode" runat="server" errormessage="Postal code is required." controltovalidate="txtPostalCode">*</asp:requiredfieldvalidator>

Remove the "*" if you don't want immediate feedback... the errormessage is displayed in the <asp:validationsummary> control when you submit the form.

like image 35
Konrad Avatar answered Sep 23 '22 07:09

Konrad


Normally it appears only when you enter text, delete it again and then move out of the textbox. I think this is by design. Try to change EnableClientScript property.

like image 36
Tim Schmelter Avatar answered Sep 21 '22 07:09

Tim Schmelter