javascript help

supajason

Member
Messages
288
Reaction score
2
Points
18
hello i need some help.

i have a page like this:

Code:
<html>
<head>
<title>Test</title>
</head>
<body>
<form name="requestform" method="post">
<input type="radio" name="question" value="yes">yes
<input type="radio" name="question" value="no">no
<br>
<input type="radio" name="something" value="yes">yes
<input type="radio" name="something" value="no">no
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
i have about 20 radio groups (each with 2 buttons) that i need to validate to make sure each of the groups has a radio button checked.

thank you for your time.
 

t2t2t

New Member
Messages
690
Reaction score
0
Points
0
Code:
<html>
<head>
<title>Test</title>
<script type="text/javascript">
<!--
function radioTest(theForm, amount){
	j = 0
	for (i=0;i<theForm.elements.length;i++) {
		if (theForm.elements[i].type == 'radio' && theForm.elements[i].checked) {
			j++
		}
	}
	if(j < amount) {
		alert('Please choose every box!');
		return false;
	}
	return true;
}
// -->
</script>
</head>
<body>
<form name="requestform" method="post" onSubmit="return radioTest(this, 2);">
<input type="radio" name="question" value="yes">yes
<input type="radio" name="question" value="no">no
<br>
<input type="radio" name="something" value="yes">yes
<input type="radio" name="something" value="no">no
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
For the 20 radio-group form, change in "radioTest(this, 2)" the number from 2 to 20.

(I know it's rather cheap way, but it works on every form thrown to it)
 
Last edited:

kundamor

Member
Messages
36
Reaction score
0
Points
6
better to create radio buttons group with loose, it will minimize your code.
 
Top