• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 366
  • Last Modified:

Radio Button Validation

Hello,
I'm trying to validate radio button as shown below.
I just need to get length of number of radio buttons in the form.
But I don't know how to reference it.
I can get the reference by specifying element name of radio button like formName.radioVal.length. However, I'd like to use this function to any form I create.
Please someone advise.
Thank you.

<script language="JavaScript" type="text/javascript">
<!--
 
function checkRadioSelected(formObj, lbl) {
 
	if (!formObj)
	    return false;
 
	var myRadio = -1;
			
	if (formObj.lbl.length == 0)
		return false;
		
	for (var x = formObj.lbl.length-1; i > -1; i--) {
		if (formObj.lbl[x].checked) {
			myRadio = i;
			i = -1;
		}
	}
	
	if (myRadio == -1) {
		alert('Nothing is selected');
		return false;
	} else {
		return true;
	}
}
 
 
<form name="myForm">
<input type="radio" name="radioval" value=1>
<input type="radio" name="radioval" value=2>
<input type="radio" name="radioval" value=3>
 
<!-- I need to use href to execute the function -->
<a href="javascript: void(0)" onclick="checkRadioSelected(myForm, 'radioVal');">
Check
</a>
 
</form>
 
 
-->
</script>

Open in new window

0
eb110k
Asked:
eb110k
  • 3
1 Solution
 
janjerellCommented:
Pass the radio object itself.

Try this:
<html>
<head>
<script language="JavaScript" type="text/javascript">
 
function checkRadioSelected(lbl) {
 
        if (!lbl)
            return false;
 
        var myRadio = -1;
 
 
        if (lbl.length == 0)
                return false;
                
	
        for (var i = lbl.length-1; i > -1; i--) {
 
                if (lbl[i].checked) {
                        myRadio = i;
                        i = -1;
                }
        }
 
 
        if (myRadio == -1) {
                alert('Nothing is selected');
                return false;
        } else {
                return true;
        }
} 
 
</script>
</head>
 
<body>
<form name="myForm">
<input type="radio" name="radioval" value=1>
<input type="radio" name="radioval" value=2>
<input type="radio" name="radioval" value=3>
 
<!-- I need to use href to execute the function -->
<a href="javascript: void(0)" onclick="checkRadioSelected(document.myForm);">
Check
</a>
 
</form>
</body>
 
</html>

Open in new window

0
 
janjerellCommented:
Sorry, it's this one:
<script language="JavaScript" type="text/javascript">
 
function checkRadioSelected(lbl) {
 
        if (!lbl)
            return false;
 
        var myRadio = -1;
 
 
        if (lbl.length == 0)
                return false;
                
	
        for (var i = lbl.length-1; i > -1; i--) {
 
                if (lbl[i].checked) {
                        myRadio = i;
                        i = -1;
                }
        }
 
 
        if (myRadio == -1) {
                alert('Nothing is selected');
                return false;
        } else {
                return true;
        }
} 
 
 
</script>
<body>
<form name="myForm">
<input type="radio" name="radioval" value=1>
<input type="radio" name="radioval" value=2>
<input type="radio" name="radioval" value=3>
 
<!-- I need to use href to execute the function -->
<a href="javascript: void(0)" onclick="checkRadioSelected(document.myForm.radioval);">
Check
</a>
 
</form>
</body>

Open in new window

0
 
eb110kAuthor Commented:
Thank you for your advise.
It worked perfectly by passing object.
0
 
janjerellCommented:
glad to help..
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now