Page.IsValid and Validate - Raj Kaimal

Page.IsValid and Validate

ASP.net ships with a couple of validator controls that allow you to determine whether the value of the input controls they are validating is valid.

Here is a simple example of a TextBox control with a RequiredFieldValidator attached and a Button control.

<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="MyValidationGroup"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
 ErrorMessage="This field is required!" ValidationGroup="MyValidationGroup"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" ValidationGroup="MyValidationGroup" />
 

Note that all controls belong to the same ValidationGroup  - a new feature of ASP.net 2.0. With JavaScript turned on, when a user clicks the button, they will see an error message displayed next to the control being validated if they fail to fill in the TextBox. (One could also use a ValidationSummary control)

With JavaScript turned off, what may not be known is that, on the server side, even though the validators fire, it is left to the developer on how to use that information.  

You may think you have built a secure application but a hacker could disable JavaScript and bypass *all* your validators! This is where the Page.Validate method and more importantly, the Page.IsValid property come in.

The Validate method is fired automatically by controls that have the CausesValidation property set to true. Note that the Button control's CausesValidation property is true by default. The Validate method iterates through all enabled validation controls on the page and validates them. This event occurs after the Load event in the page lifecycle.

If the control that raised the event has a ValidationGroup specified, then only enabled validator controls that belong to the same ValidationGroup are validated by calling the Page.Validate(ValidationGroup) overload method. As mentioned previously, this is done automatically for controls that have the CausesValidation property set to true.

The Page.IsValid property tells you whether the validation succeeded or not. It can be called only after the Page.Validiate method is called. By using this property, you can add logic to your page to determine whether to proceed with the PostBack event or not. So, in addition to relying on client side validation, it is also important that you call Page.IsValid when handling the postback event. Here is what the code behind will look like:

protected void Button1_Click(object sender, EventArgs e) {
 
//Not required since the CausesValidation property of the Button control is true by default.
//Page.Validate("MyValidationGroup");
 
//Proceed only if the vaidation is successfull
if (!Page.IsValid) {
 return;
}
 
//Insert data into SQL
 
}
 

With the ASP.net declarative programming approach, the Page.IsValid method is something easy to forget.

Since you have reached this point, here is a simple quiz ;-) In the code below, what happens when Button2 is clicked?

<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="MyValidationGroup"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="This 
field is required!" ValidationGroup="MyValidationGroup"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" 
ValidationGroup="MyValidationGroup" CausesValidation="true" />
<br />
<br />
<asp:TextBox ID="TextBox2" runat="server" ValidationGroup="AnotherValidationGroup"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2"
ErrorMessage="This 
field is required!" ValidationGroup="AnotherValidationGroup"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button2" runat="server" Text="Button" ValidationGroup="AnotherValidationGroup" 
OnClick="Button2_Click" /> 
 
protected void Button2_Click(object sender, EventArgs e) {
 
    Page.Validate("MyValidationGroup");
 
    if (!Page.IsValid) {
         return;
    }
 
    Response.Write("Button was clicked at " + DateTime.Now.ToShortTimeString());
} 

Published Thursday, March 15, 2007 2:16 PM by rajbk
Filed under: , ,

Comments

# re: Page.IsValid and Validate

Its really an amazing feature...

Thanks for article...

Friday, May 23, 2008 9:10 AM by Nishant Thakkar

# re: Page.IsValid and Validate

Thanks for sharing useful comments.

Tuesday, July 1, 2008 8:46 AM by Sabarinathan Arthanari

# re: Page.IsValid and Validate

I would like to have one "RequiredFieldValidator" block of code to manage many controls.

Where you have the statement:

ControlToValidate="TextBox1"

Can you list multiple controls (TextBox's)in this statement?  

Can one RequiredFieldValidator validate multiple textbox's?

Thursday, July 3, 2008 3:15 PM by Lane

# re: Page.IsValid and Validate

Very informative ,small article ,really helpful for a beginner like me.Thank you very much...

Monday, July 7, 2008 5:17 AM by Sobin

# re: Page.IsValid and Validate

Very good informative article...

Monday, August 25, 2008 1:17 AM by sujithmysore

# re: Page.IsValid and Validate

Does anyone know how to see (imperatively) wich controls have failed after, for example, a submit button request?

Monday, August 25, 2008 2:07 PM by Fergara

# re: Page.IsValid and Validate

It is really good.

thanks

Friday, October 24, 2008 2:15 AM by Narasimha Rao bolla

# re: Page.IsValid and Validate

Thanks man!!! new information.GOOD LUCK.Keep going

Wednesday, October 29, 2008 1:29 AM by Aby

# re: Page.IsValid and Validate

Excellent....saved me couple of hours.

Wednesday, December 10, 2008 3:22 PM by anjee

# re: Page.IsValid and Validate

Hi Raj,

Thanks for this post.

Only using the validators on the client side is not the solution for controlling the client entry. So it should be also checked on the server side. So why not us Page.Validate and Page.IsValid? Thanks, again.

Eralper

Monday, December 22, 2008 6:36 AM by eralper

# re: Page.IsValid and Validate

Nice Explanations. Thanks for Sharing

Thursday, April 30, 2009 2:14 AM by Abhijit

# re: Page.IsValid and Validate

That was most helpful.  Very clear and just what I needed.  Thanks!

Thursday, May 14, 2009 2:08 AM by Don

# re: Page.IsValid and Validate

Its siple great and helped me alo. Thanx for such useful post

Thursday, May 14, 2009 11:43 PM by AImen

# re: Page.IsValid and Validate

gud article but i got one question this page.validate is used as we just want to chk the validation on page_load

but by writng this code on button_click agn our code is going to server side

than what is the use of this code

Tuesday, June 9, 2009 8:01 AM by pupilstuff

# re: Page.IsValid and Validate

Great Explanation! Helped me a lot.. Thanks for the code

Wednesday, July 1, 2009 3:03 AM by Jayapriya

# re: Page.IsValid and Validate

Very good article! I didn't know that we had to call the Page.IsValid method on the server side, because I thought it was handled by the Framework.

Thursday, July 23, 2009 10:45 AM by Vincent Therry

# re: Page.IsValid and Validate

thanks for the post its really nice

Friday, October 23, 2009 8:20 AM by Rajesh

# re: Page.IsValid and Validate

Well, its very nice article! Keep it up my friend!!!

Ans 4 ur quiz:

the page will not be validated since u r not specify d required filed in textbox1 bcoz u r validate the group "MyValidationGroup" in button2 click event.

Pls. let me know if my ans is incorrect!!!

thanks,

Thursday, November 19, 2009 5:28 PM by jk

# re: Page.IsValid and Validate

'Really fantastic' can you tell me, do we have to use page.isvalid when we use a validation group.

Wednesday, January 6, 2010 11:22 PM by Batcha

# re: Page.IsValid and Validate

Thanks for publishing so clear way to explain Page.Validate method and Page.IsValid property of validation.

Sunday, June 6, 2010 5:22 PM by amit saini

# re: Page.IsValid and Validate

the page will not b validated, since button2 belongs to anther validationgroup..

Thursday, July 15, 2010 3:30 AM by rampalani

# re: Page.IsValid and Validate

Raj, Excellent explanation.  Look forward to more.

Saturday, July 17, 2010 1:30 AM by SG