views:

289

answers:

5

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

+3  A: 

Hey,

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

Brian
+4  A: 

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.

John K
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();
roman
Hmmmmm ... if you haven't already, try the overloaded version.Otherwise, which method or event are you calling Page.Validate from?
John K
I missed the boat on that one by forgetting about Page.IsValid.
John K
+1  A: 

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

David Stratton
+3  A: 

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>
Tj Kellie
All ASP.NET Validators provide client and server side validation - not just the CustomValidator.
Mike C.
A: 

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

roman