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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form validation

Status
Not open for further replies.

zoser

Programmer
May 9, 2002
39
US
Does anyone know how to validate a checkbox and text field together? For instance, I am creating a form that has a checkbox for the answer "Yes" and textfield for the answer "No" The question is something like this:

Do you like ice cream?

Yes (checkbox)
If No, Why? (textfield)

I would just like to validate it so that if the user completely misses the question they will get prompted to answer yes or no. I have tried a number of different combinations of code and nothing seems to work. Can someone please point me in the right direction?
 
you pretty much have to say:
if ((checkbox is not checked) && (text field is empty)) {
alert("please tell us if you like / do not like icecream")
} --------------------------------------------------
Goals are dreams with deadlines
-------------------------------------
 
This should work.

<html>
<head>
<title>Untitled</title>
<script>
function validate()
{
//check ifcheckbox and txt field weren't touched
if (document.all.c1.checked==false && document.all.txt.value==&quot;&quot;)
{ alert(&quot;Please answer the question&quot;); }
else
//The question was answered.
{ alert(&quot;Thank You&quot;) }
}
</script>
</head>
<body>
<form>
Do you like ice cream? Yes
<input type=&quot;checkbox&quot; name=&quot;c1&quot;><br><br>
If No, Why? <input type=&quot;text&quot; name=&quot;txt&quot;><br><Br>
<input type=&quot;button&quot; name=&quot;but&quot; value=&quot;check&quot; onclick=&quot;validate();&quot;>
</form>
</body>
</html>
 
thanks for the info. but i can't seem to get it to work

this is the code -- looks similar to DoubleV's but isn't working (it is part of other validation statements)

else if ( (document.initiationForm.gdg[0].checked != true) &&
(emptyField(formObj.gdgNum))
alert(&quot;Please fill in yes or no.&quot;)

what am i missing??
 
1. check that your script actually gets to the &quot;esle&quot; part.
2. if it does, then i have 2 ideas:
a. you seems to be missing 1 bracket in your condition:
Code:
if (  (document.initiationForm.gdg[0].checked != true) && (emptyField(formObj.gdgNum)) )
b. try changing 
[code]
document.initiationForm.gdg[0].checked != true
to
Code:
!document.initiationForm.gdg[0].checked
--------------------------------------------------
Goals are dreams with deadlines
-------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top