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!

Validate checkbox if...then 1

Status
Not open for further replies.

Fris

Programmer
Jun 23, 2003
22
US
I have 4 checkboxes on a form. If the user checks a box, then there MUST be a number entered its associated textbox. Can anyone help me with the proper syntax for validation?

Here is the code for one of the check/text box combinations:

<TD><INPUT TYPE=&quot;checkbox&quot; NAME=&quot;OilSample&quot; ></TD>
<TD><INPUT TYPE = &quot;text&quot; Size = &quot;4&quot; Name=&quot;OilSampleNm&quot;</TD>
<input type=&quot;hidden&quot; name=&quot;22f&quot; value=&quot;OilSampleNm&quot;>
<TD>Oil Samples on all compressors</TD>

Thanks in advance for any assistance you may be able to provide!

CS
 
why do you have the hidden field with the same name?

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
Good morning, Rick!

The information gathered by this form is submitted via email. I &quot;borrowed&quot; this part of the code from another form that we have used to gather information and it does what we need it to do. I am VERY new at this - so there's a great possibility that I'm doing it incorrectly.....

Christine
 
Ooops - I misread - the hidden's name is &quot;22f&quot;

Here's how I would check all of the checkboxes...

<script>
function checkBoxes(){
allInput = document.getElementsByTagName(&quot;input&quot;)
for (x=0; x<allInput.length; x++){
if (allInput[x].type==&quot;checkbox&quot;){
if(allInput[x].checked){
numVal = eval(&quot;document.myForm.&quot; + allInput[x].name + &quot;Nm.value&quot;)
if (isNaN(parseInt(numVal)){
alert(&quot;Please enter a number&quot;)
}
}
}
}
}
</script>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
Rick,

Thank you for your help. I am, however, having difficulty - Big Surprise! I get the following error:

Expected &quot;}&quot; on line ___ - The line that contains the code:

if (isNaN(parseInt(numVal)){

I don't understand why it is looking for another one.

Christine
 
Rick... In my last post, I indicated the error I was receiving was: &quot;Expected a curly bracket&quot; - My eyes are bad - it needed a round bracket. I added a round one to even things up and it worked perfectly!!

Thank you ever-so much for your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top