How to display the requiredfieldvalidator error message after clicking submit

an error message will appear if I exit the current text box. I don’t want to display it until I click the "Submit" button.

+5
source share
6 answers

This is not possible if ClientScript is enabled for your validators. And ClientScript is enabled by default for your validators. You need to disable this using the EnableClientScript for False options in your source.

Now in the event handler of your submit button, Page.Validate () and Page.IsValid are called to check if all validators passed 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
}
+8
source

- ...

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

:

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

"*", ... , <asp:validationsummary> .

+4

, . , . EnableClientScript.

+1

forecolor validator . onclientclick css color :

<asp:CompareValidator ID="birthdaycheck" runat="server" ErrorMessage="" 
    Text="*Required" ControlToValidate="birthday" ValidationGroup="rfi" 
    Operator="NotEqual"  ForeColor="#F3F3E9"  />  


<asp:Button ID="btnFinish" runat="server" Text="Finish"
    CausesValidation="true" CommandName="MoveComplete" CssClass="navButton" 
    ValidationGroup="rfi" 
    OnClientClick="$('#wizard_birthdaycheck').css('color','red');" />
0
source

You can set CausesValidation="False"for a button for which you do not want the check to be performed.

<asp:Button ID="btnCancel" runat="server" Text="cancel" CausesValidation="False"
                            onclick="btnCancel_Click"/>
0
source

Try this to create dynamic switches along with the required fields ...

    TableRow trow4 = new TableRow();
    trow4.Style.Add("width", "100px");
    TableCell tcel4 = new TableCell();
    Label lb4 = new Label();
    lb4.Text = Resources.QcLabelName.Gender;
    tcel4.Controls.Add(lb4);
    CSSCell(tcel4);
    table.Rows.Add(trow4);
    RadioButtonList rblist = new RadioButtonList();
    rblist.ID = "rbtnmalendfemale";
    rblist.Items.Add("Male");
    rblist.Items.Add("Female");
    tcel4.Controls.Add(rblist);
    trow4.Cells.Add(tcel4);
    table.Rows.Add(trow4);
    rblist.Visible = true;
    RequiredFieldValidator rFV5 = new RequiredFieldValidator();
    TableCell tcl46 = new TableCell();
    rFV5.ControlToValidate = "rbtnmalendfemale";
    rFV5.ErrorMessage = "Gendor Selection Is Mandatory";
    rFV5.Style.Add("color", "Red");
    rFV5.ID = "Reqfield9";
    tcl46.Controls.Add(rFV5);
    trow4.Cells.Add(tcl46);
    table.Rows.Add(trow4);
    rFV5.Visible = true;
-2
source

All Articles