simpli Posted March 15, 2009 Share Posted March 15, 2009 Hi, I am trying to validate in my form validation if a radio button was selected. I have radio buttons that give options between 2 choices and basically I want to stop the validation process if no choice has been made. I get to the part where i call the validation function but I dont seem to be executing what is in the function for some reason. Again, i am new to js I am going through the Tizag tutorial but trying the monkey see monkey do approach at the same time. Thanks for any help. My code is below, J-R <script type='text/javascript'> function formValidator(){ // Make quick references to our fields var matchup1 = document.getElementById('1vs8East'); alert("calling validation"); alert(matchup1.value); if(radioselected(matchup1, "You must choose a team")){ return true; } return false; } function radioselected(elem, helperMsg) { chosen = "" len = elem.length alert("in validation"); alert(len); for (i = 0; i <len; i++) { if (elem.checked) { chosen = elem.value } } if (chosen == "") { alert("no choice made") elem.focus(); return false; } return true } </script> <?php /*Form header information */ print <<<htmldebut <form id="frmchoixronde1" name="frmchoixronde1" method="POST" enctype="application/x-www-form-urlencoded" onsubmit='return formValidator()'> <b>Entrez vos choix pour la premiere ronde</b> htmldebut; include('textmenu.css'); echo '<html><head><title>Page des poolers</title></head> <body>'; //If we get here as a result of the submit button having been pushed: We are going to validate the data if(isset($_POST['submit'])){ echo 'Server side validation'; } ?> <?php echo '<input type="submit" name="submit" value="Soumettre vos choix">'; //Shows Eastern conference matchups echo '<br/>'; echo '<fieldset>'; echo '<legend> Est</legend>'; echo '<input name="1vs8East" type="radio" id="1vs8East_1" value="Boston">Boston</label><label for="1vs8East_2"><input name="1vs8East" type="radio" id="1vs8East_2" value="Buffalo">Buffalo</label>'; echo '</fieldset>'; //End of the form ?> </form> <br/> </body></html> Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 15, 2009 Share Posted March 15, 2009 First of all, the HTML is all out of wack! You're writing JS to the page before you have even opened the HTML tag. I've stripped out all the PHP in the code below - just use that format for generating the page <html> <head> <title>Page des poolers</title> <script type="text/javascript"> function formValidator(formObj) { if(!radioSelected(formObj['1vs8East'])) { alert("You must choose a team"); return false; } alert("Validation passes"); return true; } function radioSelected(radioGrp) { if (radioGrp.length) { //There are 2 or more options for (var i=0; i<radioGrp.length; i++) { if (radioGrp[i].checked) { return true; } } //No options were checked return false; } else { //There is only 1 option return radioGrp.checked; } } </script> </head> <body> <form id="frmchoixronde1" name="frmchoixronde1" method="POST" enctype="application/x-www-form-urlencoded" onsubmit='return formValidator(this)'> <b>Entrez vos choix pour la premiere ronde</b> <input type="submit" name="submit" value="Soumettre vos choix"> <br/> <fieldset> <legend> Est</legend> <label for="1vs8East_1"><input name="1vs8East" type="radio" id="1vs8East_1" value="Boston">Boston</label> <label for="1vs8East_2"><input name="1vs8East" type="radio" id="1vs8East_2" value="Buffalo">Buffalo</label> </fieldset> </form> </body> </html> 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.