Reputation: 4513
I have an ASP.NET webform which I want to validate Client-Side and Server-Side, using the same validation controls. I can't seem to find the solution for this - the client validation works great, but when I disable javascript - It ignores the validation.
Help would much be appreciated.
Roman
Upvotes: 6
Views: 33245
Reputation: 50728
You can always trigger validation by the validator1.Validate()
method, which will do the server-side comparison. Check Page.IsValid
to see if server-side validation isn't being performed? I think you can invoke it via Page.Validate()
.
HTH
Upvotes: 9
Reputation: 28917
Explicitly call Page.Validate() on the server side.
Or the overloaded Page.Validate(string) to target one of your validation groups.
Update:
I forgot, after you run Validate(..), check the Page.IsValid
property - it's up to you to stop the page from submitting if this property == false
.
Upvotes: 15
Reputation: 4513
Found the answer! The answer is to use Page.Validate() and then check for Page.IsValid to check if the validation was valid or not. Only using Page.Validate() didn't help - the code proceeded and didn't stop.
Thanks guys, Roman
Upvotes: 0
Reputation: 6476
Roman,
You can use the ASP.net custom validator to provide both a client and a server method for validation. That way if you disable the js you should still hit the server validation method. In this example the "ClientValidate" function would be defined in a javascript block on your page, and teh "ServerValidate" function would exist in your codebehind file.
<asp:textbox id="textbox1" runat="server">
<asp:CustomValidator id="valCustom" runat="server"
ControlToValidate="textbox1"
ClientValidationFunction="ClientValidate"
OnServerValidate="ServerValidate"
ErrorMessage="*This box is not valid" dispaly="dynamic">*
</asp:CustomValidator>
Upvotes: 3
Reputation: 73594
If you're using standard validation controls, data is always re-verified on the server even if client side validation is specified.
See the note in this article right after figure 2:, which says:
Double-Checking Client-Side Validation
One interesting point is that even though the form data is validated on the client (eliminating the need for additional requests to and responses from the server to validate the data), the data entered is re-verified on the server. After the data is checked on the client and found valid, it is rechecked on the server using the same validation rules. These are rules that you establish to ensure against some tricky programmer out there trying to bypass the validation process by posting the page to the server as if it passed validation.
http://msdn.microsoft.com/en-us/library/aa479013.aspx
However, you can force validation on the server by calling Page.Validate()
Upvotes: 3