Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RequiredFieldValidator validating multiple controls

Status
Not open for further replies.

funkydonkey2000

Programmer
May 29, 2003
12
0
0
AU
I was just wondering is it possible to assign a single RequiredFieldValidator to validate multiple controls. The reason I want to do this is because I have 10 text boxes, and I want to check if they are all filled in. If either just one box is empty or all boxes are empty, I want to just have one error message showing in the ValidationSummary control. So not 6 error messages if 6 of the boxes are emtpy.

Thanks

Tony
 
funky: Good question. Typically of course the RequiredFieldValidator is assigned the ID of the control and validates the control through its ControlToValidate property (so a 1 to 1 relationship).

I would think you could accomplish this using the IsValid property of the page itself, which will be True only if all RequiredFieldValidators are True, and if all or one are not, then Page.IsValid is false. In code it would look something like:

Code:
Sub Button_Click (s As Objecxt, e As EventArgs)
 If Page.IsValid Then
  'redirect...
 End If
End Sub

This may not however be the desired solution. Though the default validation is set to run javascript clientside the above method 'appears' to require a postback -- although I can't be absolutely sure on that -- but even so, it might still be worth using. Otherwise I would think you might have to run a bit of custom javascript to accomplish what you're trying to do. Just a thought (my laptop is down for the next couple of days so I can't run a test at the moment; but you might try that approach and see what you come up with.





 
I try your method, I had to make all my RequiredFieldValidator controls to be EnableClientScript="false". But how do I get ValidationSummary to only display one error message instead of all error message from all RequiredFieldValidator. Maybe I have to write my own javascript alert popup.
 
So I see you had to disenable client side java, which is what I expected (returning to the sever). Looks like you have 2 choices -- handle the event in code behind at the server (not a terrible choice) or add a java attribute to the submit button and alert the user if any of the textboxes are Null else redirect. A quick search here at the forum and you should be able to find an example of with the code on how to add a java attribute to a button. It really should be a fairly easy task.

I suppose another alternative you might consider is to use the summary pop-up window built into the ASP.NET validators, i.e., where all validation is reported on a single 'alert' pop up window. Perhaps in some way you can modify the bulleted list of errors here to be acceptable for your purpose. I don't have an example in front of me at the moment but a quick on line revew and you should be able to find this (also searh here at Tek-Tips as the summary validator window has been discussed a few times over the last year. Take a look at the summary alert window before you proceed to writing the java routine -- my gut instinct is that the summary window might do the trick for you.

If I were facing the problem I'd probably write a simple code behind routine to carry out what I needed -- but then if you can save a post back that would be better in the long run.

Post back and let me know what you come up with --
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top