6

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

2
  • can you give us more info? Specifically what validation controls are you using? The built-in ASP.NET validators? If so then which, of these, are you using (e.g. CustomValidator). Some snippets of pertinant code always help. Finally, are you using update panels or anything AJAX-y? Commented Jan 4, 2010 at 17:33
  • Using the regular validators, no CustomValidator at all. Nothing AJAXy, no panels ... nothing. just a plain form :) Commented Jan 4, 2010 at 18:23

5 Answers 5

15

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi, I've tried this - But still didn't work. I use the regular asp.net validators. even not the custom one. when I disable javascript - it just goes through, even if I explicitly use Page.Validate();
Hmmmmm ... if you haven't already, try the overloaded version. Otherwise, which method or event are you calling Page.Validate from?
I missed the boat on that one by forgetting about Page.IsValid.
9

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

Comments

3

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

Comments

3

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>

1 Comment

All ASP.NET Validators provide client and server side validation - not just the CustomValidator.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.