Validating only one of two controls

D

Darrel

(whew...I'm full of questions tonight!)

I have two textBoxes that I need to validate. One's a phone number and one's
an email address. I need to only validate one of them, though. (ie, only
email or only phone are required).

How can I do this? I've set up a custom control the checks both of these
fields and if they're both blank, it returns a 'one of the two must be
filled in' error.

I think I'd like to do something like this:

if textbox.text > "" then validate with regex validator

but I'm not sure how to go about that.
 
G

Guest

Darrel..place CausesValidation="False" to the button !
like:-
<asp:Button runat="server" id="YourIDButton" text="YourText"
CausesValidation="False"></asp:Button>
Hope i understand what u want!
 
P

Peter Blum

I see this problem all of the time.

The ASP.NET validators do not handle this behavior automatically. Instead,
you have to use the CustomValidator and write the logic in your own
evaluation function. The trickiest part is writing the client-side function,
when you don't know DHTML and JavaScript. Many users have not done this part
and live with a server side only validator.

I built a replacement to Microsoft's validators that handles this.
"Professional Validation And More" (http://www.peterblum.com/vam/home.aspx)
includes 22 validators. Its MultiConditionValidator is designed for this. It
lets you merge the logic of other validators together into a boolean
expression like "TextBox1 is Required OR TextBox2 is Required". It generates
the javascript for you. All javascript runs on IE, IE/Mac,
Netscape/Mozilla/FireFox, Opera 7 and Safari whereas Microsoft's only
handles the IE browsers.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 
D

Darrel

I built a replacement to Microsoft's validators that handles this.
"Professional Validation And More"
(http://www.peterblum.com/vam/home.aspx) includes 22 validators. Its
MultiConditionValidator is designed for this. It lets you merge the logic
of other validators together into a boolean expression like "TextBox1 is
Required OR TextBox2 is Required". It generates the javascript for you.
All javascript runs on IE, IE/Mac, Netscape/Mozilla/FireFox, Opera 7 and
Safari whereas Microsoft's only handles the IE browsers.

Peter:

A belated thanks for your answer! I'll definitely take a look at that.

The custom validator makes sense. Actually, in this case, I was able to get
away with having the custom validator check to make sure they aren't both
empty. Then, I validated both with a regex. The regex checks for either a
valid entry OR a blank field. So, in the end, that worked. At least for
these two particular fields.

-Darrel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top