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

checking radio button value for form validation

Status
Not open for further replies.

kevpho

Programmer
Jun 9, 2003
47
CA
Hi,

This is a quick question. Does anyone know the syntax for getting the value of a radio button within my VBScript on the same page. I need to make an if statement that says

"if (radiobutton1 is checked) then
...
else
..."

I can't figure out the correct syntax. I'm getting an error when I use the following:
"Document.formName.fieldName[1].checked"
 
when a button is checked it is set to True
if the buttons are grouped (named the same) then you loop through them checking for the True condition. If found it is checked. If they are named differently then you just validate the True/False condition

1H 1K 10 3D 3F 3E 3K 38 3J 10 1T 10 3G 3L 3I 35 10 35 3O 33 35 3C 3C 35 3E 33 35

onpnt2.gif
 
thanks for the quick reply ..

so I have "AND" and "OR" buttons and I'd like to check which one is checked when I submit the form. Depending on which one is selected, an appropriate message should prompt the user.

I found a javascript script that returns the value of the radio button online ... but I'm guessing I can't call the javascript function within my VBScript function can I?
 
yes you can call a javascript function in the vbscript function.

the javascript function needs to be the first <script> declaration in the page however.


1H 1K 10 3D 3F 3E 3K 38 3J 10 1T 10 3G 3L 3I 35 10 35 3O 33 35 3C 3C 35 3E 33 35

onpnt2.gif
 
example:
<html>
<head>

<script language=&quot;javascript&quot;>
function checkRadio() {
var msg = &quot;&quot;;
var frm = document.forms[0];

for(x=0;x<frm.length;x++) {

if(frm.elements[x].type==&quot;radio&quot;) {
if(frm.elements[x].checked==true)
msg += &quot;Form element: &quot; +frm.elements[x].name + &quot; checked\n&quot;;
}
}
alert(msg);
}
</script>

<script language=&quot;vbscript&quot;>
Function startStrucutre

Dim str : str = &quot;Function will now be called&quot;
alert str

checkRadio()

End Function


</script>
</head>
<body>
<form>
<input type=&quot;radio&quot; name=&quot;radGrp1&quot;>Check1
<input type=&quot;radio&quot; name=&quot;radGrp1&quot;>Check2
<input type=&quot;radio&quot; name=&quot;radGrp1&quot;>Check3
<input type=&quot;radio&quot; name=&quot;radGrp1&quot;>Check4
<input type=&quot;radio&quot; name=&quot;radGrp1&quot;>Check5
<input type=&quot;radio&quot; name=&quot;radGrp1&quot;>Check6

<input type=&quot;radio&quot; name=&quot;radGrp2&quot;>Check1
<input type=&quot;radio&quot; name=&quot;radGrp2&quot;>Check2
<input type=&quot;radio&quot; name=&quot;radGrp2&quot;>Check3
<input type=&quot;radio&quot; name=&quot;radGrp2&quot;>Check4
<input type=&quot;radio&quot; name=&quot;radGrp2&quot;>Check5
<input type=&quot;radio&quot; name=&quot;radGrp2&quot;>Check6

<input type=&quot;button&quot; value=&quot;click&quot; onClick=&quot;startStrucutre()&quot;>
</form>
</body>
</html>

1H 1K 10 3D 3F 3E 3K 38 3J 10 1T 10 3G 3L 3I 35 10 35 3O 33 35 3C 3C 35 3E 33 35

onpnt2.gif
 
alrite i'll give it a try and hopefully it comes out ok ... thanks
 
Your sample script works great. Explains exactly what I needed. Thanks a bunch onpnt !
 
[SMILE]

1H 1K 10 3D 3F 3E 3K 38 3J 10 1T 10 3G 3L 3I 35 10 35 3O 33 35 3C 3C 35 3E 33 35

onpnt2.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top