how to make requiredfieldvalidator error message display after click submit button
Asked Answered
L

6

6

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.

Lear answered 18/5, 2010 at 6:43 Comment(0)
U
9

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
}
Urbana answered 18/5, 2010 at 6:52 Comment(0)
D
4

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.

Disaffiliate answered 18/5, 2010 at 17:38 Comment(0)
P
2

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.

Practicable answered 18/5, 2010 at 6:49 Comment(0)
H
1

Set the forecolor property of the validator to the background color of your page. Then in the onclientclick of the submit button, change the css color property to the desired 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');" />
Hispid answered 14/6, 2012 at 18:54 Comment(0)
P
0

You could set CausesValidation="False" for the button for which you don't want validation to happen.

<asp:Button ID="btnCancel" runat="server" Text="cancel" CausesValidation="False"
                            onclick="btnCancel_Click"/>
Platas answered 31/8, 2012 at 4:43 Comment(0)
L
-2

Try this one for creating dynamic radio buttons along with required field validators...

    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;
Lesterlesya answered 7/11, 2014 at 15:58 Comment(1)
it would be great to have some comments or other hints what the code you posted is doing. Please provide some explanations in future posts.Service

© 2022 - 2024 — McMap. All rights reserved.