[Javascript] Validating a radio button

Hakan M. hakan at backbase.com
Thu Jan 15 03:29:03 CST 2004


This should work.

for(i=0; i<document.form.carshare.length; i++) {
	if(document.form.carshare[i].checked == true){
		alert(document.form.carshare[i].value + ' selected.');
	}
}

Chris Tifer wrote:
> Remember, radio buttons are collections (or arrays) of elements so you need
> to loop through each possible choice to see if it's checked.
> 
> Chris Tifer
> http://emailajoke.com
> 
> ----- Original Message ----- 
> From: <richf at ukonline.co.uk>
> To: <javascript at LaTech.edu>
> Sent: Wednesday, January 14, 2004 3:38 PM
> Subject: [Javascript] Validating a radio button
> 
> 
> Hi all,
> 
> I use this script to error check some drop down boxes which works fine e.g.
> modefrom. But how can I use the same syntax for the radio buttons I have
> e.g.
> carshare ? None of the following bits for the radio button carshare work !
> 
> Thanks in advance for any help.
> 
> Richard
> 
> <script LANGUAGE="JavaScript">
> function checkFields() {
> missinginfo = "";
> 
> if(document.form.modefrom.value == "-1") {
> missinginfo += "\n     -  How your child travels from school";
> }
> if(document.form.carshare.checked) {
> missinginfo += "\n     -  Whether you would car share";
> }
> if(document.form.carshare.checked == false) {
> missinginfo += "\n     -  Whether you would car share";
> }
> if(document.form.carshare.selected == false) {
> missinginfo += "\n     -  Whether you would car share";
> }
> 
> if (missinginfo != "") {
> missinginfo ="_____________________________\n" +
> "You failed to correctly fill in your:\n" +
> missinginfo + "\n_____________________________" +
> "\nPlease re-enter this information \nand press the submit button";
> alert(missinginfo);
> return false;
> }
> else return true;
> }
> </script>
> 
> ----------------------------------------------
> This mail sent through http://www.ukonline.net
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
> 
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
> 
> .
> 




More information about the Javascript mailing list