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

Form Validation - mixed elements

Status
Not open for further replies.

jsprgrmr

Technical User
Mar 12, 2002
6
US
Hi I have a form with a question that must be answered Before the form is submitted. There are 4 possible answers to the question 3 radio buttons and 1 text field.

If none of the radio buttons are selected then the text field must be populated BEFORE the form can be submitted.

validating that one of the radio buttons are selected is no problem, however; verifying that the text box is populated is driving me nuts.

Here is my current code. I have commented out (//) the line I was trying to use to check for the text box being populated if no radio buttons were checked. tia

<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859&quot;>
<style>
body{cursor:crosshair,color:red;}
</style>
<script language=&quot;JavaScript&quot;>
function RadioCheck(){

csfOs=classSurFrm.other_22.value;
itok1=false;
for(it=0; it< classSurFrm.ITEM_19.length;it++){
if(classSurFrm.ITEM_19[it].checked)itok1=true;
//if(csfOs==&quot;&quot; && itok1==&quot;false&quot;);
}
if(!itok1){
alert('You must answer #19. Please try again');
event.returnValue=false;
}



}
</script>
</head>

<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>
<form name=&quot;classSurFrm&quot; ACTION='success.html' METHOD='POST'onSubmit=&quot;RadioCheck();&quot;>
<div align=&quot;center&quot;>

<table width=&quot;50%&quot; border=&quot;0&quot; cellspacing=&quot;4&quot; cellpadding=&quot;4&quot;>
<tr>
<td>
<input type=&quot;radio&quot; name=&quot;ITEM_19&quot; value=&quot;ITEM_19|Manager&quot;>
Manager </td>
</tr>
<tr>
<td>
<input type=&quot;radio&quot; name=&quot;ITEM_19&quot; value=&quot;ITEM_19|Co-Worker&quot;>
Co-worker </td>
</tr>
<tr>
<td>
<input type=&quot;radio&quot; name=&quot;ITEM_19&quot; value=&quot;ITEM_19|Website&quot;>
Website </td>
</tr>
<tr>
<td valign=&quot;middle&quot;>Other source:
<input type=&quot;text&quot; name=&quot;other_22&quot; size=&quot;30&quot;>
</td>
</tr>
</table>
</div>
<input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Submit&quot;><input type=&quot;Reset&quot; name=&quot;Reset&quot; value=&quot;Reset&quot;>
</form>
</body>
</html>
 
The quickest simplest answer would be to have a fourth radio button next to the text box that automatically gats selected as soon as you enter text in the box.

Keith
 
Thanks for the input Keith, However; I would still have to validate that the text area is populated. I've tried several veriations of &quot;if&quot; statements. I either get a syntax error, or the validation out right fails.
 
First of all, try this:

if( (csfOs==&quot;&quot;) && (itok1==&quot;false&quot;) )

Second, take this line out of the [tt]for[/tt] loop and place it after:

for(it=0; it<classSurFrm.ITEM_19.length; it++)
if(classSurFrm.ITEM_19[it].checked)
itok1=true;

if ((csfOs==&quot;&quot;) && (itok1==&quot;false&quot;))
alert('You must answer #19. Please try again');

Try it and tell what happens.
good luck
 
Starway, Thank you!

I had to make one adjustment. The way you wrote it allowed the form to submit with no input. I changed => if ((csfOs==&quot;&quot;) && (itok1==&quot;false&quot;)) to =>if((csfOs==&quot;&quot;) &&(!itok1)) with this change an answer must be given in order for the form to submit.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top