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

MessageBoxes and Validation

I frequently see questions about how to show an alert with error messages when the user submits the page, or how to get a confirmation message to work with validation on the page.

Alert on submit

To show an alert on submit, add the ValidationSummary control and set its ShowMessageBox property to true. If you don't want to show the ValidationSummary control on the page itself, set its ShowSummary property to false.

<asp:ValidationSummary id="Summary1" runat=server ShowMessageBox="true" ShowSummary="false" />

Be aware that the message box requires client-side code support. So any validator that only has server-side code will not appear in the message box.

Confirm message

To add a confirm message, you add the following javascript to the onclick event of the button:

if (!confirm('[your message here]')) return false;

In ASP.NET 2.0, assign that string to the button's OnClientClick property.

In ASP.NET 1.1, assign that string to the button's onclick attribute in Page_Load, like this:

Button1.Attributes.Add("onclick", "if (!confirm('[your message here]')) return false;")

In ASP.NET 1.0, there are some tricks to doing this but it's quite complex.


View Entire Article

User Comments

Title: fsdfs   
Name: sdfsd
Date: 2012-12-19 3:34:55 AM
Comment:
sdfsdf
Title: gfgf   
Name: gf
Date: 2012-12-08 1:34:49 PM
Comment:
gf
Title: tyty   
Name: tyty
Date: 2012-10-31 6:24:26 AM
Comment:
rtyrtyy
Title: sfdgsdf<   
Name: dsf
Date: 2012-10-25 11:20:47 AM
Comment:
c
Title: title   
Name: name
Date: 2012-10-12 5:03:07 PM
Comment:
xxx
Title: show the clander when i click in textbox in asp.net   
Name: prabh mangat
Date: 2012-05-05 3:43:34 AM
Comment:
pls give me the code or help me.....
Title: tooooooooo pooooooooooooor code   
Name: Rahul
Date: 2012-04-11 1:39:02 PM
Comment:
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
Comment:
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
Comment:
Hi,
Thanks grea888 post. u saved my day.
Title: Question   
Name: AYA
Date: 2009-12-30 10:05:08 AM
Comment:
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
Comment:
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
Comment:
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
Comment:
hi,
i am a beginner with asp.net 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
Comment:
One of The Best Article !!!
Title: JC   
Name: James
Date: 2009-07-21 8:03:09 PM
Comment:
Excellent Artical..thanks!
Title: thank you   
Name: Mark
Date: 2009-06-16 9:35:49 AM
Comment:
Very nice work, thank you. :)
Title: .net2.0 TextBox.CausesValidation   
Name: Jian
Date: 2009-03-16 2:37:40 AM
Comment:
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
Comment:
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
Comment:
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:

{
ValidatePage();
if (Page_IsValid)
dosomething();
else
dosomeotherthing();
}

Sadly there is no equvivalent to ValidateDate()
Title: Thanks!   
Name: Jane
Date: 2008-10-28 6:26:03 AM
Comment:
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
Comment:
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
Comment:
just i want to know thE asp.net FAQ'S
Title: Thanks a lot...   
Name: Shebin
Date: 2008-09-12 2:28:34 AM
Comment:
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
Comment:
Thanks for this article. It solved my problem.
Title: Good!!   
Name: Sathish Kumar (Dhanus Technologies)
Date: 2008-03-25 7:45:12 AM
Comment:
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
Comment:
hi,
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
Comment:
Not bad.
Title: Cool   
Name: Tim Stover
Date: 2007-12-31 6:05:59 AM
Comment:
great
Title: Thanks so much!   
Name: CH
Date: 2007-11-23 3:49:44 AM
Comment:
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
Comment:
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
Comment:
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
Comment:
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
Comment:
Really a nice article which help me to learn a lot
Title: Thanq   
Name: Anil
Date: 2007-07-26 5:48:29 AM
Comment:
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
Comment:
As I have been working on a quite large e-CRF and used validators, while being (relatively ) new to asp.net. 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
Comment:
thnks ur site provides boost to developers
Title: Displaying alert or confirm after validation   
Name: Rotem
Date: 2007-03-29 10:50:54 AM
Comment:
For those of you who would like to display an alert/confirm message AFTER validation...check out this link:

http://aspalliance.com/357_Add_Confirmation_After_Controls_Validation_on_Client

now go fourth and validate!!!
Title: peer   
Name: lopez
Date: 2007-03-20 9:38:58 AM
Comment:
Excelent article.
Title: Cheers   
Name: Da Koda Code
Date: 2007-03-13 11:15:31 AM
Comment:
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
Comment:
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
Comment:
Really Thanks ..for this
Title: Wonderful   
Name: Randy
Date: 2007-02-15 6:32:32 PM
Comment:
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
Comment:
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
Comment:
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
Comment:
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
Comment:
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
Comment:
Your article was a great help. Thanks so much.
Title: Thank you so much!!!   
Name: Marisa
Date: 2006-10-26 11:19:58 AM
Comment:
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
Comment:
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
Comment:
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
Comment:
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
Comment:
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
Comment:
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
Comment:
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
Comment:
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
Comment:
Its an excellent article.
Title: Extremely cool article   
Name: tientrinh
Date: 2006-07-03 7:00:30 AM
Comment:
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
Comment:
Earlier controls were not validated at fire fox
Now it works with simple tric of server side validation
thanx
Title: Very Good Article   
Name: Santosh Kumar Devaki
Date: 2006-06-20 1:42:44 AM
Comment:
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
Comment:
Hi,
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
pankaja_shankar@ml.com

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

Thanks a lot for the support.

cnmayyanar@yahoo.com
Title: menu control using javascript in asp.net   
Name: shri
Date: 2006-04-11 7:54:10 AM
Comment:
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
Comment:
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
Comment:
No Use Now
Title: Thanks :)   
Name: Targos
Date: 2006-01-27 1:04:52 PM
Comment:
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
Comment:
thanks for the help
Title: Is Alert possible with ASP.Net 2   
Name: Abhishek dave
Date: 2005-12-24 6:22:25 AM
Comment:
hi,
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

regards

abhishek
Title: Excellent Article   
Name: Manoj
Date: 2005-12-13 1:23:16 AM
Comment:
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
Comment:
Really helpful for .Net developers.From this i came to know all the properties of Validators of ASP.net...WONDERFUL!!!!!!!.......Thanks....
Title: Mr   
Name: Dave Sexton
Date: 2005-11-30 5:42:21 AM
Comment:
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
Comment:
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 gs_manjotra@yahoo.com

Thanks
Title: programmer   
Name: s1
Date: 2005-11-25 1:12:20 AM
Comment:
quiet good for the beginers
Title: Explains it all.   
Name: Andy
Date: 2005-11-15 6:44:26 PM
Comment:
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
Comment:
Very good.It solves my problem
Title: Good Work   
Name: Nauman
Date: 2005-11-09 6:48:15 AM
Comment:
Very helpful material.
Title: Just Superb!!   
Name: Sandesh kp
Date: 2005-11-05 5:06:52 AM
Comment:
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
Comment:
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
Comment:
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
Comment:
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
Comment:
No more explanation needed about page validation.

Totally helpful
Title: Cool   
Name: Vikrant Kaul
Date: 2005-10-17 5:00:47 AM
Comment:
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
Comment:
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
Comment:
i just say what a beautiful article
Title: Great Article   
Name: James
Date: 2005-09-18 3:27:23 PM
Comment:
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
Comment:
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
Comment:
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
Comment:
this is not quite information to learn asp.net
must have provided a lot.

see u soon
Title: superb   
Name: ankit
Date: 2005-09-13 8:23:18 AM
Comment:
this helped me a lot to learn asp.net.
thanks a lot.
Title: Just what I needed   
Name: Jenny
Date: 2005-08-27 12:22:31 AM
Comment:
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.
Fantastic
Title: prgmer   
Name: Kathy
Date: 2005-08-24 2:48:22 PM
Comment:
Excellent!! Helped Me a lot:-)
Title: Programmer   
Name: Jim
Date: 2005-08-21 11:28:55 PM
Comment:
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
Comment:
I am a new user of asp. net . i want to know that how to use class in asp.net using script language vb.net
Title: Great article   
Name: Jon Paal
Date: 2005-08-05 4:38:52 PM
Comment:
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 ASPAlliance.com  |  Page Processed at 2024-10-08 7:42:10 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search