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 element validation question

Status
Not open for further replies.

GSXR179

Programmer
Oct 11, 2001
34
US
Hi,

Does anyone know how to validate a text box if a radio button is checked?

Here's the situation.

I have multiple pairs of radio buttons on a page yes/no I need to ensure that for a choice of no, the user fills out a comments text box for the given no answer.

I know this seems simple but I am really stuck.

Thanks in advance,

Mike
 
haven't tested this but i think this should work:

function checkThingie() {
if (document.yourForm.yourradiobutton.value == 'No') {
if (document.yourForm.yourtextfield.value == "") {
alert("You need to enter something in the text box")
return false;
}
}
}

put the function into your form on the onSubmit event like this:

<form name=&quot;whatever&quot; onSubmit=&quot;return checkThingie()&quot;>

hope this helps!

Tony
 
before testing the radio button's value, i think you have to, um, check it to see if it has been checked (if you know what i mean)

there are a whole bunch of useful javascript code snippets at
in fact, this one seems to be quite apropos --

Q1039 How can I ensure that one radio button is checked and that the relevant text field is not empty?

rudy
 
The above code does work on a defined number of radio boxes with static values.

However, the page that I'm working on has dynamic radio buttons, as I have listed below.

<SCRIPT Language=&quot;Javascript&quot;>
function checkComment() {
if (document.myForm.a#myQuery.id#.value == 1) {
if (document.myForm.COMMENT_#myQuery.ID#.value == &quot;&quot;) {
alert(&quot;You need to enter a description in the comments box for selection of no&quot;)
return false;
}
}
}
</script>
<cfform action=&quot;form_act.cfm&quot; name=&quot;myForm&quot; onSubmit=&quot;return checkComment()&quot;>
<cfloop action=&quot;myQyery&quot;>
<cfinput name=&quot;a#myQuery.id#&quot; type=&quot;radio&quot; value=&quot;1&quot; required=&quot;yes&quot; message=&quot;Please make a selection&quot;>Yes

<cfinput name=&quot;a#myQuery.id#&quot; type=&quot;radio&quot; value=&quot;2&quot; required=&quot;yes&quot; message=&quot;Please make a selection&quot;>No

<textarea name=&quot;COMMENT_#myQuery.ID#&quot; cols=&quot;&quot; rows=&quot;&quot; name=&quot;thisText&quot;></textarea>
<cfloop>
</cfform>
I'm having a problem of calling up the function. I have tyiesd naming the function with the unique ID number but with only one form I don't know how to get it to fire.

Thanks for all of your help on this.

Mike

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top