...

View Full Version : Radio Button Validation



bubble00000
06-16-2010, 01:05 PM
Wondering how come this doesn't work any ideas? its validating that the radio buttons have been selected.


if ( ( form.q1[0].checked == false ) && ( form.q2[1].checked == false) && ( form.q3[2].checked == false )) { alert ( "Please Choose a " ); return false; }

mattyod
06-16-2010, 01:14 PM
Um surely you should be using check-boxes here.

A group of radios should always have at least one member checked, they are an either or mechanism.

You can validate against the selected value of a group of radios, what you want to be doing is validating if check-boxes are checked.

Philip M
06-16-2010, 01:55 PM
To validate radios:-


<form name= "myform">
<input type = "radio" name = "rad1" value = "NE">North East
<input type = "radio" name = "rad1" value = "NW">North West
<input type = "radio" name = "rad1" value = "SE">South East
<input type = "radio" name = "rad1" value = "SW">South West
<input type = "radio" name = "rad1" value = "Midlands">Midlands
<br><br>
<input type = "button" value = "Which Radio Selected?" onclick = "chkrads()">
</form>

<script type = "text/javascript">

function chkrads() {
var chosen = "None";
var which = -1;
var len = document.myform.rad1.length;
for (i = 0; i <len; i++) {
if (document.myform.rad1[i].checked) {
chosen = document.myform.rad1[i].value;
which = i;
}
}
if (chosen == "None") {
alert("No Location Chosen");
}
else {
alert("You selected " + chosen + " which is Radio Button " + which );
}
}

</script>

To validate checkboxes:-


<form name = "myform">
<input name = "myname1" type = "checkbox" value = "Red"> RED <br>
<input name = "myname2" type = "checkbox" value = "Blue"> BLUE <br>
<input name = "myname3" type = "checkbox" value = "Green"> GREEN <br>
<input name = "myname4" type = "checkbox" value = "Yellow"> YELLOW<br><br>
<input type = "button" value = "Click Me" onclick="cboxlist()"><br>
</form>

<script type = "text/javascript">

function cboxlist(){
var choice = "Your colour choice is: ";
var x = "";
for (var i=1; i<5; i++) {
x = document.myform['myname' + i].value;
if (document.myform['myname' + i].checked) {
choice = choice + " " + x ;
}
}
if (choice.length <= 24) {
choice = choice + "NONE";
}
alert (choice);
}

</script>


Quizmaster: The Mississippi flows through almost the entire length of which country?
Contestant: Egypt.

bubble00000
06-16-2010, 02:24 PM
Thanks for that, finally got it working (y) .. :thumbsup:

Old Pedant
06-16-2010, 08:43 PM
A group of radios should always have at least one member checked, they are an either or mechanism.


*NOT TRUE*. Unless you specify the CHECKED property for one of the radio buttons in your HTML, then it is quite possible that *NONE* of the buttons will be checked by the user.

A group of radio buttons will have either ZERO or ONE button checked.

mattyod
07-29-2010, 11:20 AM
*NOT TRUE*. Unless you specify the CHECKED property for one of the radio buttons in your HTML, then it is quite possible that *NONE* of the buttons will be checked by the user.

A group of radio buttons will have either ZERO or ONE button checked.

Note the use of the word "should" in my original post :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum