3

In ASP.NET 2, I've used Field Validators, and RequiredField validators, but I'm unsure of how to handle a case like this.
I have two check boxes on a page, and I need to be sure that at least one of them is set. So, if you look at in binary, it can be 01, 10 or 11, but it can not be 00. My question is, what the best way to do this with checkboxes?

Can the normal ASP Validators handle this, or would I need to create an integer value like mentioned above, hidden somewhere and use a RangeValidator do a test to make sure THAT value is never zero?

5 Answers 5

8

Worst case you can write a CustomValidator the can do whatever you like. Sounds like what you need is along the lines of:

isValid = Check1.Checked | Check2.Checked

Sign up to request clarification or add additional context in comments.

1 Comment

Just to point out to others that might read this, you have to do a "arguments.IsValid = (CheckBoxAccident.Checked | CheckBoxTrafficViolation.Checked);", but you had the exact right idea. Thanks man!
4

Use CustomValidator

2 Comments

Yep. That did the trick. I would have selected your answer, but Ian had a little more info in his, even though I already understood how you validate two bool values, I wanted others to be able to understand it. But thank you for the link, it came in handy, and was exactly what I needed.
@JonPeterson I tried editing the link, but the answer is too short (19 characters, while SO apparently enforces 30), so it refuses to accept the changes.
3

This control (written by me) supports CheckBox and CheckBoxList:

http://www.codeproject.com/KB/validation/AtLeastOneOfValidator.aspx

Just add it to visual studio, drop it on your page, and add your checkboxes to it's Controls list. It will work like any other validator control.

2 Comments

Thanks Joel.. You know I'm gonna download this and check it out. I was able to do what I needed with a custom validator, but if I had to do even more validation on it, it may not do what I want...
The advantage of this over a custom validator is that you get client-side validation without writing any javascript.
1

Custom validator is the obvious solution. Also, when using a custom validator you should also check for validity on the server side just in case the javascript fails due to some reason.

P.S.: Don't always trust what the client(browser) sends you.

1 Comment

I can't seem to make the client side work in this case, only the server side. Because they are asp:CheckBox's I'm validating, I have to omit the ControlToValidate....
0

Here is an article I wrote regarding this exact issue. I also wanted to validate multiple controls which was easy using the CustomValidator but one of the issues I was not happy about is that if you corrected the validation issue the error did not go away until you posted back.

I figured out a way to hide the error message and revalidate and wrote a small blog entry about it. Check it out and see what you think.

http://coding.infoconex.com/post/ASPNET-CustomValidator-that-validates-multiple-controls-using-both-Server-Side-and-Client-Side-scripting.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.