[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Radio Button Validation

Posted on 2009-05-12
4
Medium Priority
?
365 Views
Last Modified: 2012-05-06
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
Comment
Question by:eb110k
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 3

Expert Comment

by:janjerell
ID: 24371478
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
 
LVL 3

Accepted Solution

by:
janjerell earned 2000 total points
ID: 24371488
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
 

Author Closing Comment

by:eb110k
ID: 31580839
Thank you for your advise.
It worked perfectly by passing object.
0
 
LVL 3

Expert Comment

by:janjerell
ID: 24372037
glad to help..
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question