checkbox checker

Discussion in 'JavaScript' started by jcnkty, Jul 19, 2007.

  1. #1
    newbie here guys...

    I have 3 checkbox form field and i need a script to prompt for error if user doesn't click on any of the 3 cbox's.
     
    jcnkty, Jul 19, 2007 IP
  2. nfd2005

    nfd2005 Well-Known Member

    Messages:
    295
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    130
    #2
    
    <script language="javascript" type="text/javascript">
    function checkbox() {
    
    var checklength=document.myForm.needs.length;  // This is the total number of checkboxes you have.
    var thebox;
    var checked = false; // By default, lets just say that no checkboxes have been checked.
    
    var i=0;
    
    for (i=0;i<=checklength;i++) {   // Lets loop through all the checkboxes called needs.
    
    thebox=document.myForm.needs[i];
    
    if (thebox.checked == true){   //If this needs box is checked, let's note it.
    checked=true;
    return (true);
    }
    
    
    if (i == checklength - 1) {   // Is this the last checkbox to check?
    
    if (checked == false) {  // If there are no checked boxes, lets alert the user...
    alert("Please Make A Selection.");
    return (false);  // Return to the page and don't finish the submission process.
    }else{
    return (true);
    }
    
    
    }
    }
    
    }
    </script>
    <form onsubmit="return checkbox();" name="myForm">
    
    <input type="checkbox" name="needs" value="Web Design" />Web Design<br />
    
    <input type="checkbox" name="needs" value="Web Hosting" />Web Hosting<br />
    
    <input type="checkbox" name="needs" value="Web Promotion" />Web Promotion<br />
    
    <input type="submit" value="Submit" />
    </form>
    
    
    Code (markup):
    Hope this helps.
     
    nfd2005, Jul 19, 2007 IP
  3. jcnkty

    jcnkty Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks.. one more... how about with same question (3 checkbox) with a list menu?
     
    jcnkty, Jul 21, 2007 IP
  4. nfd2005

    nfd2005 Well-Known Member

    Messages:
    295
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    130
    #4
    <script language="javascript" type="text/javascript">
    function checkbox() {
    if (document.myForm.mylist.value == '') {
    alert("Please select from the dropdown menu.");
    return(false);
    }
    var checklength=document.myForm.needs.length;  // This is the total number of checkboxes you have.
    var thebox;
    var checked = false; // By default, lets just say that no checkboxes have been checked.
    
    var i=0;
    
    for (i=0;i<=checklength;i++) {   // Lets loop through all the checkboxes called needs.
    
    thebox=document.myForm.needs[i];
    
    if (thebox.checked == true){   //If this needs box is checked, let's note it.
    checked=true;
    return (true);
    }
    
    
    if (i == checklength - 1) {   // Is this the last checkbox to check?
    
    if (checked == false) {  // If there are no checked boxes, lets alert the user...
    alert("Please Make A Selection.");
    return (false);  // Return to the page and don't finish the submission process.
    }else{
    return (true);
    }
    
    
    }
    }
    
    }
    </script><form onsubmit="return checkbox();" name="myForm">
    
    <input type="checkbox" name="needs" value="Web Design" />Web Design<br />
    
    <input type="checkbox" name="needs" value="Web Hosting" />Web Hosting<br />
    
    <input type="checkbox" name="needs" value="Web Promotion" />Web Promotion<br /><br />
    <select name="mylist">
    <option selected="selected"></option>
    <option value="Web Design">Web Design</option>
    <option value="Web Hosting">Web Hosting</option>
    <option value="Web Promotion">Web Promotion</option>
    </select><br />
    <br />
    
    
    <input type="submit" value="Submit" />
    </form>
    Code (markup):
    I'm assuming you meant a select list dropdown... Hope this helps.
     
    nfd2005, Jul 22, 2007 IP
  5. jcnkty

    jcnkty Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thanks!


    last one... what if the field names of the checkbox's are different? how is the javascript coding of it?
     
    jcnkty, Jul 22, 2007 IP
  6. in2clearsky

    in2clearsky Peon

    Messages:
    121
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Try this.
    
    	function funcCheck()
    	{
    		var isOK = false
    		var y = document.getElementsByTagName("input");
    		for (i=0;i<y.length;i++)
    		{
    			if(y[i].type=="checkbox"&&y[i].checked==true) {isOK = true;}
    		}
    		if(isOK)
    		{
    			alert("OK");
    		}
    		else
    		{
    			alert("Error");
    		}
    	}
    
    Code (markup):
     
    in2clearsky, Jul 24, 2007 IP