Reputation: 995
I have a form with a bunch of fields that I am using RequiredFieldValidator, RegularExpressionValidator, and CustomValidator.
I want my form to perform client side checks when tabbing between fields (it currently does this), but I want to force a server side submit when the asp:button is clicked. Right now, if a form field is determined invalid on the client side, the submit button doesn't do anything. I want it to submit the page and perform the server side check.
The reason I want this to happen is because I want the page to go back to the top and display all possible issues and make it obvious to the user that there was a problem. Currently if they didn't see the client side error message, they may just click submit, see nothing happen, and end up confused.
Example field on aspx Page:
<asp:TextBox MaxLength="30" ID="LNom" runat="server" /><asp:RequiredFieldValidator ID="reqLNom" runat="server" ErrorMessage="Last Name Required" ControlToValidate="LNom" /><asp:CustomValidator ID="valLNom" runat="server" ErrorMessage="Please enter a valid last name (less than 30 characters in length)." ControlToValidate="LNom" OnServerValidate="ValidationLastName" />
<asp:Button ID="Submit" Text="Submit" runat="server" onclick="Submit_Click" />
Submit button Code Behind:
protected void Submit_Click(object sender, EventArgs e)
{
Page.Validate();
if (Page.IsValid)
{
// Do stuff
}
}
Obviously there is a bit more to this, but you get the idea.
Upvotes: 0
Views: 2564
Reputation:
You can do this client side, no need for a post back. Look into the ValidationSummary control:
http://www.w3schools.com/aspnet/control_validationsummary.asp
Upvotes: 1
Reputation: 1741
If you add 'CausesValidation="False"' to your asp:button declaration. This will cause the postback to happen regardless of the outcome of client side validation
Upvotes: 1
Reputation: 1266
Try asp.net ValidationSummary
. See http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.validationsummary(v=vs.110).aspx
It does exactly how you want it. It can be a pop up or inline notification that tells the user what s/he needs to fix.
Upvotes: 1