jesushax Posted October 13, 2008 Share Posted October 13, 2008 Hi all, as the title heopfully explains i have to checkboxes on my form if neither of them are checked id like to display an error message and dont submit until one of them has a tick in it can anyone help me out? Cheers Link to comment https://forums.phpfreaks.com/topic/128162-check-form-for-input-in-two-checboxes-if-both-blank-dont-submit/ Share on other sites More sharing options...
JasonLewis Posted October 13, 2008 Share Posted October 13, 2008 Yup, give your checkboxes different ID's. <input type="checkbox" name="checkbox1" id="checkbox1" value="check1_value" /><br /> <input type="checkbox" name="checkbox2" id="checkbox2" value="check2_value" /> Then make a JavaScript function. <script language="javascript"> function checkboxes(){ var check1 = document.getElementById("checkbox1"); var check2 = document.getElementById("checkbox2"); if(check1.checked || check2.checked){ return true; }else{ alert("Please check a box!"); return false; } } </script> Then in you're submit button you need to call the function but with a return beforehand. <input type="submit" name="submit" id="submit" onclick="return checkboxes();" /> Good luck. Link to comment https://forums.phpfreaks.com/topic/128162-check-form-for-input-in-two-checboxes-if-both-blank-dont-submit/#findComment-663817 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.