[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Validate checkboxes at least two should be selected

Posted on 2009-05-01
2
Medium Priority
?
282 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:MickeC
2 Comments
 
LVL 51

Accepted Solution

by:
Steve Bink earned 1000 total points
ID: 24277240
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
 

Author Closing Comment

by:MickeC
ID: 31576795
Perfect, thanks!!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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

829 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