Validate checkboxes at least two should be selected

Hi! I have a problem with validate. I want to use my old validate script so i included a part that i found on the net for validating checkboxes. And it almost worked. I want to validate that you have to check at least two boxes, but in my script it enough with to check one, but it should be two, what are i doing wrong??
Also do you know how i also in the same validating can check that no more than two is checked. I want only two checkboxes to be checked! Look at my code below.
<script language="JavaScript" type="text/javascript">
 
<!--
function echeck(str) {
  var at="@"
  var dot="."
  var lat=str.indexOf(at)
  var lstr=str.length
  var ldot=str.indexOf(dot)
  if (str.indexOf(at)==-1){
     alert("Incorrect mailaddress!")
     return false
  }
  if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
     alert("Incorrect mailaddress!")
     return false
  }
  if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
      alert("Incorrect mailaddress!")
      return false
  }
   if (str.indexOf(at,(lat+1))!=-1){
      alert("Incorrect mailaddress!")
      return false
   }
   if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
      alert("Incorrect mailaddress!")
      return false
   }
   if (str.indexOf(dot,(lat+2))==-1){
      alert("Incorrect mailaddress!")
      return false
   }
   if (str.indexOf(" ")!=-1){
      alert("Incorrect mailaddress!")
      return false
   }
   return true          
}
 
 
function MCValidation()
{
 
if (document.mcform.frmFirstname1.value == "") {
 
alert("Firstname is missing!");
document.mcform.frmFirstname1.focus();
return false;
 
}
 
var emailID=document.mcform.frmEmail1
  
  if ((emailID.value==null)||(emailID.value=="")){
    alert("Email is missing!")
    emailID.focus()
    return false
  }
  if (echeck(emailID.value)==false){
    emailID.value=""
    emailID.focus()
    return false
  }
  
  if (document.mcform.frmBekraftaEmail1.value != document.mcform.frmEmail1.value){
		document.mcform.frmBekraftaEmail1.focus();
		alert("Email and verify email is not a match!");
		return false
	}	
	
	//CHECKBOX LEAST 2
var boxesTicked = "" 
for (i = document.getElementsByName('seminar').length - 1; i >= 0; i--) { 
if (document.getElementsByName('seminar')[i].checked) { 
boxesTicked = boxesTicked + document.getElementsByName('seminar')[i].value + "\n" 
} 
} 
if (boxesTicked <2 ) { 
alert ("You have to choose two.") 
return false 
}
 
return true;
 
}
 
// -->
</script>

Open in new window

MickeCAsked:
Who is Participating?
 
Steve BinkCommented:
Try this change:
//CHECKBOX LEAST 2
var boxesTicked = 0
for (i = document.getElementsByName('seminar').length - 1; i >= 0; i--) { 
  if (document.getElementsByName('seminar')[i].checked) { 
    boxesTicked++;
  } 
} 

Open in new window

0
 
MickeCAuthor Commented:
Perfect, thanks!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.