ainoy31 Posted August 7, 2007 Share Posted August 7, 2007 i have a form that requires to either check a yes or no box. i need to validate that a box has been checked. here is what i have but not working. function check() { if(document.flight_ticket.yn.checked == false) { alert ("Please check YES or NO to confirm flight"); return false; } return true; } much ty. Ainoy Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 7, 2007 Share Posted August 7, 2007 This is a PHP forum, you are wanting Javascript. How exactly can you check yes or check no for a single check box? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 7, 2007 Share Posted August 7, 2007 Well this is completely a javascript issue, but, i've no idea what the problem is. Your javascript code is fine. I can only assum its to do with the way you call it. Try: <html> <head> <script type="text/javascript"> function check() { if(document.flight_ticket.yn.checked == false) { alert ("Please check YES or NO to confirm flight"); return false; }else{ return true; } } </script> </head> <body> <form name="flight_ticket" onSubmit="return check()"> <input type="checkbox" name="yn" /><br /> <input type="submit" name="submit" /> </form> </body> </html> EDIT: How exactly can you check yes or check no for a single check box? Good point. I was assuming the idea was to see if the checkbox had been clicked, even if the error message is somewhat strange. Quote Link to comment Share on other sites More sharing options...
NArc0t1c Posted August 7, 2007 Share Posted August 7, 2007 Just a correction to you're script ben: <html> <head> <script type="text/javascript"> function check() { if(document.flight_ticket.yn.checked == false) { alert ("Please check YES or NO to confirm flight"); return false; }else{ return true; } } </script> </head> <body> <form name="flight_ticket" onsubmit="check()"> <input type="checkbox" name="yn" /><br /> <input type="submit" name="submit" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
ainoy31 Posted August 7, 2007 Author Share Posted August 7, 2007 this is the input part: p>Please confirm: <input type = "checkbox" name = "yn" value = "NO"> NO <input type = "checkbox" name = "yn" value = "YES"> YES</p> Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 7, 2007 Share Posted August 7, 2007 You should not use checkboxes. Checkboxes are meant for independant options that are either true or false. Plus you have given them the same name, which can complicate things as well. You should use a radio group instead which will only allow one of the options to be selected: HTML Please confirm: <input type="radio" name = "yn" value="NO"> NO <input type="radio" name = "yn" value="YES"> YES JavaScript function check() { ynfield = document.flight_ticket.yn; if (!ynfield.options[0].checked && !ynfield.options[1].checked) { alert ("Please select YES or NO to confirm flight"); return false; } return true; } Quote Link to comment Share on other sites More sharing options...
ainoy31 Posted August 7, 2007 Author Share Posted August 7, 2007 thanks for the help. however, took ur last suggestion and it still does not check if a radio button is checked or not. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 7, 2007 Share Posted August 7, 2007 I misreferenced the radio buttons: function check() { ynfield = document.flight_ticket.yn; if (!ynfield[0].checked && !ynfield[1].checked) { alert ("Please select YES or NO to confirm flight"); return false; } return true; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.