Solving the Challenges of ASP.NET Validation
page 4 of 7
by Peter Blum
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 117070/ 86

When to Validate

There are several situations that require you to disable one or more validators, or to request validation where it's not normally done:

  • Some buttons do not save anything, such as a Cancel button. You need to prevent the button from validating.
  • There are two or more buttons on the page and each has its own list of validators to fire.
  • A validator should be evaluated based on some condition on the page, such as the value of another control.
  • AutoPostBack should validate to avoid a post back if the page has errors.

Preventing a button from validating

Here is one of the most overlooked features of ASP.NET Validation. I see developers ask about this several times a week on the forums. Now you don't have to be one of them.

When you have a Cancel button or any other button that should never validate anything, simply set its CausesValidation property to false. The Button, LinkButton, ImageButton and the HtmlControl buttons all have this property.

<asp:Button id="CancelBtn" runat=server Text="Cancel" CausesValidation="false" />

Several buttons associated with their own validators

Suppose you have a login or search section on a page that also has some data entry fields. Each section has its own submit button with validators. When you setup validators on all the fields and click one submit button, the validators from the other section report errors, preventing the form from submitting.

Microsoft recognized that ASP.NET needs to handle this case has introduced Validation Groups in ASP.NET 2.0. (My Professional Validation And More already has Validation Groups.) To use validation groups, create a group name for each section. Then assign that name to the ValidationGroup property found on ASP.NET 2.0 buttons and validators in that section. It's very simple.

What happens if you have ASP.NET 1.x? You will have to use server-side validation when the page is submitted to validate the individual validators associated with the submit button:

  1. Set the submit control's CausesValidation property to false.
  2. In the Click event method for each submit button, call the Validate() method on each validator that is in the section. Then test that the IsValid property of each validator in the section is true. For example:
if (RequiredFieldValidator1.IsValid && RequiredFieldValidator2.IsValid)
   // your code here
If RequiredFieldValidator1.IsValid And RequiredFieldValidator2.IsValid Then
   ' your code here
End If

Now this design prevents client-side validation from occuring when the user clicks submit. (Validation will still occur as the user changes individual fields.) Some users have attempted to create client-side code that runs when the user clicks on the submit button and sets each validator’s enabled attribute to false or true. That way, the client-side validation will still run, but will skip the validators that are not enabled. This is a bad design as it leaves those validators disabled. If there was an error, the user would continue to edit the form with some of the validators now disabled.

Enabling validators

Microsoft designed ASP.NET validators so you can write server-side code to solve the shortcomings of client-side code. One of those shortcomings is a very common situation: you have a validator that should not validate unless there is a specific condition on the page. Examples include a checkbox is marked, another textbox has data, and the data entry field is enabled. In my product, I solved this by providing each validator with a property called Enabler where you specify the rule that enables the validator.

When using Microsoft's validators, there are several ways to do this.

1. Use server-side validation. Call individual validator's Validate() method in the Click event method after determining if the validator should fire.

a. Turn off client-side validation on each validator that needs to be disabled by setting EnableClientScript to false.

b. Set CausesValidation to false on the submit button.

c. In the Click event method, call the Validate() method on each validator but add logic to skip any validator that should not validate. Then test the IsValid property on each validator for true before saving.

Here is an example where RequiredFieldValidator2 should be enabled only when CheckBox1 is checked. RequiredFieldValidator1 will always validate:

if (CheckBox1.Checked)
bool vIsValid = true;
if (!RequiredFieldValidator1.IsValid)
   vIsValid = false;
if (vIsValid && CheckBox1.Checked && !RequiredFieldValidator2.IsValid)
   vIsValid = false;
if (vIsValid)
   // your code here
If CheckBox1.Checked Then
End If
Dim vIsValid As Boolean = True
If Not RequiredFieldValidator1.IsValid Then
   vIsValid = False
End If
If vIsValid And CheckBox1.Checked And Not RequiredFieldValidator2.IsValid Then
   vIsValid = False
End If
If vIsValid Then
   ' your code here
End If

2. Use a CustomValidator. This will let you preserve client-side validation by creating some client-side code for validation. The problem is that you end up writing the same logic already created in the existing validators. While developing the same logic as in the RequiredFieldValidator is easy (remember to trim spaces!), try writing a CompareValidator for dates. (As the author of a popular DatePicker control, I can assure you that date parsing in Javascript is not easy!)

There's one other challenge here: you will be writing JavaScript and using DHTML. If you lack experience in these technologies, this becomes a much bigger task.

See the .NET documentation topic, CustomValidator Overview, for details on using a CustomValidator. Be sure to write code for both client and server-side validation.

AutoPostBack and validation

In ASP.NET 1.x, the AutoPostBack features of data entry controls do not validate before submitting. If they did, the page could avoid an unnecessary trip to the server.

ASP.NET 2.0 introduces two new properties of data entry controls: CausesValidation and ValidationGroup. These are exclusively used when AutoPostBack is true. When CausesValidation is true, it will perform client-side validation using the validation group defined in the ValidationGroup property.

Be careful in using this feature because all validators of a Validation Group are evaluated. So any controls that have not been filled in yet will have their RequiredFieldValidator fired, preventing post back.

View Entire Article

User Comments

Title: fsdfs   
Name: sdfsd
Date: 2012-12-19 3:34:55 AM
Title: gfgf   
Name: gf
Date: 2012-12-08 1:34:49 PM
Title: tyty   
Name: tyty
Date: 2012-10-31 6:24:26 AM
Title: sfdgsdf<   
Name: dsf
Date: 2012-10-25 11:20:47 AM
Title: title   
Name: name
Date: 2012-10-12 5:03:07 PM
Title: show the clander when i click in textbox in   
Name: prabh mangat
Date: 2012-05-05 3:43:34 AM
pls give me the code or help me.....
Title: tooooooooo pooooooooooooor code   
Name: Rahul
Date: 2012-04-11 1:39:02 PM
I want to know the confirm box will display after validation...............................
Title: Thanks and Question   
Name: rajesh
Date: 2010-04-13 5:46:29 PM
One peculiar thing I find here is.

When I use like this it works
OnClientClick==”if (!confirm('[your message here]')) return false;”
But when I put the same code in a function and call the function here, it does not work.
Lets say, I put the above code in a function foo(); and use it as below
OnClientClick=" foo();” -- Cancel or Ok both postbacks without client validation.
OnClientClick="return foo();” -- Cancel works but Ok postbacks without client validation.

Strange. Or I lack info .
Title: Thanks   
Name: suhas
Date: 2010-04-07 7:24:00 AM
Thanks grea888 post. u saved my day.
Title: Question   
Name: AYA
Date: 2009-12-30 10:05:08 AM
Hi there,

first of all thank you very much for your clear illustration,

but i faced a problem with validation,

here is my question

how can i do validation on the formview (when i click Update)
Title: Best   
Name: komal
Date: 2009-10-21 7:05:37 AM
Thank u So Much It"s Help me to Solve my Problem.
Title: This help me a lot...   
Name: sunil kumar mishra
Date: 2009-10-20 5:13:44 AM
Thanks a lot...
I was in a big problem... , I used a confirm message while finally save the payment details in University pannel. But it neglected the validation.....

Through this article i am able to do so....

Thank You
Title: Give me advice   
Name: anu
Date: 2009-09-23 2:53:25 AM
i am a beginner with now i want to develop
a web side just for improving skill but i don't know
how i will start work
Title: Thanks a million   
Name: user
Date: 2009-09-04 3:23:55 AM
One of The Best Article !!!
Title: JC   
Name: James
Date: 2009-07-21 8:03:09 PM
Excellent Artical..thanks!
Title: thank you   
Name: Mark
Date: 2009-06-16 9:35:49 AM
Very nice work, thank you. :)
Title: .net2.0 TextBox.CausesValidation   
Name: Jian
Date: 2009-03-16 2:37:40 AM
TextBox.CausesValidation is a new property in .net2.0 and the default value is false. Remember to set it to true if you want valication running when postback.
Title: Error   
Name: sameer
Date: 2009-02-05 1:41:12 PM
there is a bug when we use both of them I am using confirm when i submit to prompt the user if ok or cancel and the validation summary as well, but this is what happens

when i use return confirm([message]) it prompts the user with ok or cancel but doesn;t validate, any thoughts
Title: An unresolved issue   
Name: Andrey
Date: 2008-12-02 2:28:03 PM
I have been lookin a lot to find a solution for a simple problem.
How does one trigger validation from javascript on client side.
I would like to erite something like:

if (Page_IsValid)

Sadly there is no equvivalent to ValidateDate()
Title: Thanks!   
Name: Jane
Date: 2008-10-28 6:26:03 AM
That answers all sorts of questions that I didn't previously know enough to ask.
Title: Thank you!   
Name: Jorge Rojas
Date: 2008-09-20 8:55:28 PM
Your suggestions about supporting globalization in validators really work. Thanks a lot.
Title: ASP.NET   
Name: Ravindra
Date: 2008-09-17 4:51:58 AM
just i want to know thE FAQ'S
Title: Thanks a lot...   
Name: Shebin
Date: 2008-09-12 2:28:34 AM
I am beginer in this ASP.NET field....
Whwn I started with ASP.NET I have got a lot of doubts..
I got solution for a lot of doubts from this article...
Thanks a lot....
Title: Thanks   
Name: Jibi
Date: 2008-05-08 4:06:25 AM
Thanks for this article. It solved my problem.
Title: Good!!   
Name: Sathish Kumar (Dhanus Technologies)
Date: 2008-03-25 7:45:12 AM
Good..! Expect lot for begineers
Title: How to give validation for post-box number, mobile number in india,   
Name: pavan dhakate
Date: 2008-03-07 4:38:15 AM
i have some problem regarding giving validation to post box number n mobile/telephone numeber in india
plz give some regular expressions to solve this problems
Title: super   
Name: sakthi
Date: 2008-02-19 1:30:24 AM
Not bad.
Title: Cool   
Name: Tim Stover
Date: 2007-12-31 6:05:59 AM
Title: Thanks so much!   
Name: CH
Date: 2007-11-23 3:49:44 AM
I finally found this article that saved me!

I was unable to make my validation work after adding the confirm box!

Imagine I was stuck for 2 weeks? assigning hidden fields, postbacks...etc etc...

This article should be carved out on a piece of rock and displayed for posterity!

Thanks so much!
Title: 1.1 is the Worst!   
Name: Joe Gakenheimer
Date: 2007-10-11 3:44:44 PM
I have had it with the 1.1 version, I mean having to check each validation at the server to trip the event handler is nonsense. I have a project that I am doing maintenance on and all I needed was to add a Button Link; this app is predominately button links. Well about a minute after I put it into production, my buttons all of a sudden come to life as validators. Naturally, this did not occur in Firefox, but I had to go through a rush a build out with the buttons commented out and replaced with HTML links.

The 2.0 ASP.Net is worlds better and I can't wait to get on a good project; plus the validations work, along with ValidationGroup.
Title: This is so great!!!!!!!!!   
Name: Samantha
Date: 2007-09-05 3:28:36 AM
This is so so great! thanks a bunch...i've been spending few days trying to get the validation to work on my site...bam! only 1 line n it works miraculously! great site!
Title: Nicely Done!!   
Name: Masquinyo
Date: 2007-08-03 9:58:39 AM
What an excelent article, it has helped me with my struggles.
Title: Great Great Great   
Name: Prashanat Bhopale
Date: 2007-08-01 5:35:58 AM
Really a nice article which help me to learn a lot
Title: Thanq   
Name: Anil
Date: 2007-07-26 5:48:29 AM
Wow, what a wonderful article. You solved my biggest head aches. Thank you very much!!!!!!!!!!!!!!!!!
Title: Thank you.   
Name: Peter
Date: 2007-07-03 10:32:18 AM
As I have been working on a quite large e-CRF and used validators, while being (relatively ) new to And I must say that your article really claryfied alot.

So, thank you!
Title: great   
Name: iru
Date: 2007-04-09 6:09:36 AM
thnks ur site provides boost to developers
Title: Displaying alert or confirm after validation   
Name: Rotem
Date: 2007-03-29 10:50:54 AM
For those of you who would like to display an alert/confirm message AFTER validation...check out this link:

now go fourth and validate!!!
Title: peer   
Name: lopez
Date: 2007-03-20 9:38:58 AM
Excelent article.
Title: Cheers   
Name: Da Koda Code
Date: 2007-03-13 11:15:31 AM
This is nutzzz. I spent 1/2 a day debugging this stuff and all I need to add was the statement you recommmended

if (Page.IsValid)

Cheers dude. Wish I had spotted this earlier. Sweeeeett!!1
Title: Thanks   
Name: musmanjaved
Date: 2007-03-10 2:59:17 PM
hello sir,

Your site gives great help.Best of luck!.

Thanks n take care.
M Usman Javed.
Title: Its Great   
Name: Yogesh Mahajan
Date: 2007-03-09 4:28:55 AM
Really Thanks ..for this
Title: Wonderful   
Name: Randy
Date: 2007-02-15 6:32:32 PM
Wow, what a wonderful article. You solved my biggest head aches. Thank you very much!!!!!!!!!!!!!!!!!
Title: THANX!!!   
Name: Daniel Hofmann
Date: 2007-01-08 10:38:10 AM
After spending several days on the problem that empty textboxes are not validated, this article solved my problem. Thank you!
Title: Great Help   
Name: Vinod Kumar Sasaram(Kus)
Date: 2006-12-13 4:46:25 AM
It is an excellent site for help.
Please also give some code with sql server that help in project development
Title: group validate   
Name: siva
Date: 2006-11-22 12:50:14 AM
i want group validate in vb code.
Title: Such A gr8 Article,useful to all   
Name: Sanjay Sharma
Date: 2006-11-10 12:54:58 AM
This is really a gr8 article and it helps me a lot to learn about validators. one more thing i want to ask that if i want two diff messages in the validation summary control- one for required field and other for compare validators on the single button click.
Title: thanks you   
Name: RemoBrian
Date: 2006-11-08 5:27:35 PM
Your article was a great help. Thanks so much.
Title: Thank you so much!!!   
Name: Marisa
Date: 2006-10-26 11:19:58 AM
I've been struggling with the 2.0 validator for several weeks now. On the new version of Firefox the validation code was called AFTER my submit button's on_click event, very frustrating, because my database calls were going through before it put up the error message or had a chance to stop them. I finally realized it was calling server-side validation, but couldn't figure out what to do to resolve my issue.

Your article explained how I can check for validation in my button.

Thank you so much!!!
Title: Well presented   
Name: Sreehari
Date: 2006-10-19 5:20:17 AM
The info is really breif and well presented.
Title: Buttons do not submit(Great Article)   
Name: Farrukh Mehmood
Date: 2006-10-12 12:18:21 AM
Thanks Paul Wilson and Thomas Freudenberg for posting such information on net.for last two week i am facing this problem and i become to much tied to find the solution of this problem.
at last visit this link and find the solution thanks once again

Farrukh Mehmood
Title: Good   
Name: Merlin
Date: 2006-09-25 3:09:27 AM
You are doing a gr8 job here..Keep up ur good works..
Title: Need a little more...   
Name: Jportela
Date: 2006-09-18 12:28:23 PM
I'm having a curious issue, some of my users see the validators message when a missing field is required or when a regular expression is not matched, but even they see the message and also a validation summary, they can still submit the form. Anyway this article is good.
Title: How can I avoid postback before validation???   
Name: Joar
Date: 2006-07-18 5:51:04 AM
If I add a OnClientClick="return confirm('Save?');" to a submit button, the validation I have specified in the validation control is done AFTER the postback to the server!!!
How can I avoid that!!!
The point is not to postback before validation!!!
Title: Great article   
Name: on Paal
Date: 2006-07-10 8:05:34 AM
This helps a lot. It also points to why .net is not only hard to learn, but with all the glitches like validation
Title: Very useful article   
Name: Sudhir Kumar
Date: 2006-07-06 8:07:00 AM
I was facing problem that my web form was not submitting and not validating. But i got very good solution on this web site. Now submit button is working as well as client validation is also working.

In Last! This is very gud ARTICLE.

Thanks to author.
Title: Excellent   
Name: snehal
Date: 2006-07-04 7:50:31 AM
Its an excellent article.
Title: Extremely cool article   
Name: tientrinh
Date: 2006-07-03 7:00:30 AM
Thank you very much, good articles.
Cảm ơn rất nhiều, bài viết này quá hay.
Title: it solves my problem   
Name: Iqbal singh
Date: 2006-07-03 1:15:42 AM
Earlier controls were not validated at fire fox
Now it works with simple tric of server side validation
Title: Very Good Article   
Name: Santosh Kumar Devaki
Date: 2006-06-20 1:42:44 AM
This is a very good article, it helps me a lot. I solved many problems by studying this article
Title: Validation in ASP.Net   
Name: Pankaja
Date: 2006-06-10 2:36:20 PM
The article is interesting, I have to go back to work on monday to see if the steps mentioned in the article are followed. The scenario is, I have 4 image link buttons on the screen, only one of them has causes validation set to true. All 4 of them have enabled and visible (design time) properties set to true. But when the page is rendered none of the buttons show up. Sometimes a few tries of tabbing from field to field(which causes autopostback), the buttons appear. This has become a critical isssue for us as our product is in the User Acceptance phase and we are running on a busy server. Any help is greatly appreciated. Please respond to

Title: Validation Controls   
Name: Swaroop
Date: 2006-05-02 8:06:10 AM
Really a very good article !! Thank you
Title: Great Help   
Name: neoms21
Date: 2006-04-26 8:57:29 AM
It helped me a lot.
Kudos for writing such a article.
Title: Mr   
Name: Mani
Date: 2006-04-18 6:56:11 AM
This article is excellent and very useful those who are not familiar with .Net.

Thanks a lot for the support.
Title: menu control using javascript in   
Name: shri
Date: 2006-04-11 7:54:10 AM
i need coding for this project. Any one pls help me
Title: If ... then ... validatiors   
Name: Filip
Date: 2006-03-16 5:03:21 AM
HI there,

How do you handle situations where validation of a certain "group" is dependant on another control(s) value. For instance - only validate if I have entered my email address if I have opted to sign up for something (i.e. check a checkbox of some kind)
Title: we are already in asp.net2   
Name: gopinath
Date: 2006-02-20 3:59:57 AM
No Use Now
Title: Thanks :)   
Name: Targos
Date: 2006-01-27 1:04:52 PM
That's great. But if i would like to use validationsumary messagebox with a button which use onclientclick confirm. The sumuray messagebox is display after the confirm box...
Can i manage the order ?
Title: Fantastic   
Name: Ralph Carney
Date: 2006-01-05 6:55:14 AM
thanks for the help
Title: Is Alert possible with ASP.Net 2   
Name: Abhishek dave
Date: 2005-12-24 6:22:25 AM
IN .net framework 2 i need to alert the error message
using the given validators,
I have read a ppt from microsoft that in .net 2.0 there can be no hand-editing of WebUiValidator.js and I can't even find it as its encapsulated in webresource.axd
So Mr Peter Blum.
How to handle this challenge,
please help me out in this


Title: Excellent Article   
Name: Manoj
Date: 2005-12-13 1:23:16 AM
This is really very very usefull article for all level of software developer on .NET technology. Thanks!
Title: Great Article   
Name: Chital
Date: 2005-12-10 5:11:33 AM
Really helpful for .Net developers.From this i came to know all the properties of Validators of!!!!!!!.......Thanks....
Title: Mr   
Name: Dave Sexton
Date: 2005-11-30 5:42:21 AM
My hosting company updated to SP1 and then none of my forms worked - but thanks to this article I have fixed the problem. Many thanks.
Title: Very Useful & Important   
Name: Gurvinder Singh Manjotra
Date: 2005-11-30 1:56:34 AM
Very funny as I thought that validation work only in IE browser but this supports me a lot in doing same as Server Validation in other browsers also. My email as

Title: programmer   
Name: s1
Date: 2005-11-25 1:12:20 AM
quiet good for the beginers
Title: Explains it all.   
Name: Andy
Date: 2005-11-15 6:44:26 PM
For anyone looking to get an understanding of what the validation controls do and don't do, this is the article to read.

.Net 2 solves a lot of the problems everyone will be pleased to know.
Title: Superb   
Name: ABC
Date: 2005-11-11 2:39:27 AM
Very good.It solves my problem
Title: Good Work   
Name: Nauman
Date: 2005-11-09 6:48:15 AM
Very helpful material.
Title: Just Superb!!   
Name: Sandesh kp
Date: 2005-11-05 5:06:52 AM
Its just superb article for beginner as well as proffesionals. Thanks a lot for giving such article
Title: thanks for sharing your knowledge   
Name: neildotnet
Date: 2005-11-01 12:19:50 AM
Excellent article, i erroneously assumed the server side validation was handled automatically! this was by reading other tutorials/articles which brushed on the issue, but stated that server side validation would always occurr regardless. Working on IE also made me assume server side validation was occuring, when it must have been the client side.
Title: Very Good   
Name: RaviKumar
Date: 2005-10-25 9:12:52 AM
It is a very good article teaching many points about validators which can be implemented with no effort.
Title: What if....   
Name: Liam
Date: 2005-10-19 4:18:42 AM
Not bad....but what if we have a control that for whatever reason dosn't support CausesValidation, in our case the TabStrip control. How do we force the validation to kick-in client side before the SelectedIndexChanged event fires?
Title: Just Thank You!!   
Name: Goran
Date: 2005-10-17 3:11:20 PM
No more explanation needed about page validation.

Totally helpful
Title: Cool   
Name: Vikrant Kaul
Date: 2005-10-17 5:00:47 AM
Thanks for that great summary presentation. It was excellent help for me.
Title: Nice Article   
Name: sundaramurthy.J
Date: 2005-10-04 3:40:42 AM
Before i used javascript validation for popup message after seeing this article now iam started to using the Validation controls
Thanks for explaination
Title: Best Best Best for validation   
Name: Mohsen Eslamifar
Date: 2005-09-27 6:11:16 AM
i just say what a beautiful article
Title: Great Article   
Name: James
Date: 2005-09-18 3:27:23 PM
Thanks for the article. The automatic call to validate the page on buttons and such was driving me nuts.

Of course it had to be something so simple as an attribute. Why didn't I think of looking for that?
Title: Two sets of controls on the same page   
Name: Nanda
Date: 2005-09-15 9:01:12 PM
Ur page answers my question. Thank u very much
Title: Two sets of controls on the same page   
Name: Nanda
Date: 2005-09-15 6:41:58 PM
I haave two sets of controls on the same page. The user can select either one. say ..Register new Or Login. But the validation controls fire for both making it hard to leave the other set free..How do i manage this? ASP.NET 1.1
Title: very bad   
Name: peter
Date: 2005-09-13 8:26:15 AM
this is not quite information to learn
must have provided a lot.

see u soon
Title: superb   
Name: ankit
Date: 2005-09-13 8:23:18 AM
this helped me a lot to learn
thanks a lot.
Title: Just what I needed   
Name: Jenny
Date: 2005-08-27 12:22:31 AM
This is great. No text book in the world can replace knowledge. Was using calendar controls to assist in validation but now can make my pages much less ugly.
Title: prgmer   
Name: Kathy
Date: 2005-08-24 2:48:22 PM
Excellent!! Helped Me a lot:-)
Title: Programmer   
Name: Jim
Date: 2005-08-21 11:28:55 PM
Awesome! Just awesome! Thank you so much for making this info easy to consume.
Title: Very Nice   
Name: Anuj kumar
Date: 2005-08-19 3:45:25 AM
I am a new user of asp. net . i want to know that how to use class in using script language
Title: Great article   
Name: Jon Paal
Date: 2005-08-05 4:38:52 PM
This helps a lot. It also points to why .net is not only hard to learn, but with all the glitches like validation, it's also frustrating.

thanks for the explanantion, in spite of the shameless plug.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-07-25 12:39:40 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search