asp.netrequiredfieldvalidator

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.


Solution

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