Solved

javascript validate multiple radio buttons

Posted on 2008-10-08
3
1,156 Views
Last Modified: 2012-05-05
Have a bunch of radio buttons on an ASP page:
q1 with 4 buttons (possible answers)
q2 with 2 buttons
q3 with 4 buttons
etc.

Wondering if there is a quick way of implementing a Javascript function to ensure that all questions have been answered.

Keep in mind that we have approx. 12 pages with 10 questions per page!
0
Comment
Question by:markm5i
[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 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 22675375
refer to the attached example:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">
 
<html>
<head>
<title></title>
<script type="text/javascript">
function getElementsByName(f,n){
	var e=new Array();
	for(var i=0; i < f.elements.length; ++i)
	{
		if(f.elements[i].name==n)
			e[e.length]=f.elements[i];
	}
return e;
}
function validate(f)
{
	var radioList="q1,q2,q3,q4";
	var messages={
		"total":4
		,"q1":"Question 1 is a required field"
		,"q2":"Question 2 is a required field"
		,"q3":"Question 3 is a required field"
		,"q4":"Question 4 is a required field"
	};
 
	radioList=radioList.split(",");
	for(var i=0; i < radioList.length; ++i)
	{
		var chk=false;
		var t = getElementsByName(f,radioList[i]);
		for( var j=0; j < t.length; ++j)
		{
			if(t[j].checked)
			{
				chk=true;
				break;
			}
		}
		if( chk )
		{
			delete(messages[ radioList[i] ]);
			--messages.total;
		}
	}
	for( k in messages)
	{
		if(k!="total")
			alert(messages[k]);
	}
return (messages.total==0);
}
</script>
</head>
<body>
<form onsubmit="return validate(this)">
<div>Q1:...
	<input type="radio" name="q1" value=""/>
	<input type="radio" name="q1" value=""/>
	<input type="radio" name="q1" value=""/>
	<input type="radio" name="q1" value=""/></div>
<div>Q2:...
	<input type="radio" name="q2" value=""/>
	<input type="radio" name="q2" value=""/></div>
<div>Q3:...
	<input type="radio" name="q3" value=""/>
	<input type="radio" name="q3" value=""/>
	<input type="radio" name="q3" value=""/></div>
<div>Q4:...
	<input type="radio" name="q4" value=""/>
	<input type="radio" name="q4" value=""/>
	<input type="radio" name="q4" value=""/>
	<input type="radio" name="q4" value=""/></div>
	
	<input type="submit"/>
</form>
</body>
</html>

Open in new window

0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 22676305
Not trying to poach your question, but is this not a LOT simpler (and it handles a single radio)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">
 
<html>
<head>
<title></title>
<script type="text/javascript">
var radioList="q1,q2,q3,q4,q5".split(",");
 
function getChecked(rad) {
  if (!rad.length) return rad.checked;
  for (var i=0;i<rad.length;i++) if (rad[i].checked) return true;
  return false
}
function validate(f) {
  for(var i=0; i < radioList.length; ++i) {
    var chk=false;
    var rad = f.elements[radioList[i]];
    if (!getChecked(rad)) {
      alert('Please check at least one radio in question ' + radioList[i])
      if (rad.length) rad[0].focus(); else rad.focus();
      return false
    }
  }
  return true;
}
</script>
</head>
<body>
<form onsubmit="return validate(this)">
<div>Q1:...
	<input type="radio" name="q1" value=""/>
	<input type="radio" name="q1" value=""/>
	<input type="radio" name="q1" value=""/>
	<input type="radio" name="q1" value=""/></div>
<div>Q2:...
	<input type="radio" name="q2" value=""/>
	<input type="radio" name="q2" value=""/></div>
<div>Q3:...
	<input type="radio" name="q3" value=""/>
	<input type="radio" name="q3" value=""/>
	<input type="radio" name="q3" value=""/></div>
<div>Q4:...
	<input type="radio" name="q4" value=""/>
	<input type="radio" name="q4" value=""/>
	<input type="radio" name="q4" value=""/>
	<input type="radio" name="q4" value=""/></div>
<div>Q5:...
	<input type="radio" name="q5" value=""/>
</div>
	
	<input type="submit"/>
</form>
</body>
</html>

Open in new window

0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses
Course of the Month10 days, 11 hours left to enroll

628 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