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.
<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.
<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.
thanks! last one... what if the field names of the checkbox's are different? how is the javascript coding of it?
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):