Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
170
rated 0 times [  172] [ 2]  / answers: 1 / hits: 60620  / 14 Years ago, tue, december 14, 2010, 12:00:00

Hi All
I have a group of check box having same name so as to get the array of single variable when it is posted on serverside for exampleL



<input type=checkbox name=midlevelusers[] value=1>
<input type=checkbox name=midlevelusers[] value=1>
<input type=checkbox name=midlevelusers[] value=1>
<input type=checkbox name=midlevelusers[] value=1>


I need a javascript validation to check whether any checkbox is selected or not?



Thanks and Regards



NOTE: I need javascript validation


More From » checkbox

 Answers
9
<form name=myform method=POST action= onsubmit=return checkTheBox();>
<input type=checkbox name=midlevelusers[] value=1 /> 1 &nbsp;&nbsp;
<input type=checkbox name=midlevelusers[] value=2 /> 2 &nbsp;&nbsp;
<input type=checkbox name=midlevelusers[] value=3 /> 3 &nbsp;&nbsp;
<input type=checkbox name=midlevelusers[] value=4 /> 4 &nbsp;&nbsp;
<input type=checkbox name=midlevelusers[] value=5 /> 5 &nbsp;&nbsp;
<input type=submit value=Submit Form />
</form>

<script type=text/javascript>
function checkTheBox() {
var flag = 0;
for (var i = 0; i< 5; i++) {
if(document.myform[midlevelusers[]][i].checked){
flag ++;
}
}
if (flag != 1) {
alert (You must check one and only one checkbox!);
return false;
}
return true;
}
</script>

[#94612] Saturday, December 11, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lara

Total Points: 462
Total Questions: 100
Total Answers: 102

Location: Jersey
Member since Mon, Jun 14, 2021
3 Years ago
lara questions
;