simpli Posted March 16, 2009 Share Posted March 16, 2009 Hi, I have a form which i am trying to validate. There are txtfields and radio buttons. The text field validates fine. However when i try to validate the radiobutton nothing works. I am using document.getElementById() to get the text fields and formObj[] to get the radio buttons. They don't seem to work well together but I do not know if that is caused by something else in my script or some javascript specific issue. Can you tell me what is wrong below? Also is there a reason to use one function over the other? Thank you kindly, J-R <script type='text/javascript'> function formValidator(){ // Make quick references to our fields var p10 = document.getElementById('player_10'); var matchup1 = formObj['1vs8East']; // Validation of the text fields! if(notEmpty(p10, "Votre Joueur #10 n'a pas ete choisi")){ if(radioselected(matchup1)){ return true; } } return false; } function notEmpty(elem, helperMsg){ if(elem.value.length == 0){ alert(helperMsg); elem.focus(); // set the focus to this input return false; } return true; } function radioSelected(radioGrp) { alert("in validation"); if (radioGrp.length) { //There are 2 or more options for (var i=0; i<radioGrp.length; i++) { if (radioGrp.checked) { return true; } } //No options were checked return false; } else { //There is only 1 option return radioGrp.checked; } } </script> <form id="frmchoixronde1" name="frmchoixronde1" method="POST" enctype="application/x-www-form-urlencoded" onsubmit='return formValidator()'> <html><head><title>Page des poolers</title></head> <body> <b>Entrez vos choix pour la premiere ronde</b> <br/><fieldset><legend>East</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><br/> </fieldset><br/><fieldset><b><label for="player_10" style="width:2em">10</label></b><input name="player_10" id="player_10" type="text" size="30"><br/></fieldset><input type="submit" name="submit" value="Soumettre vos choix"></form> <br/> </body></html> Quote Link to comment Share on other sites More sharing options...
Adam Posted March 16, 2009 Share Posted March 16, 2009 Without looking too much into the code, this looks like it's your problem: var matchup1 = formObj['1vs8East']; .. Try: var matchup1 = document.forms['1vs8East']; Adam Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 16, 2009 Share Posted March 16, 2009 if(elem.value.length == 0){ Should be: if(elem.value.length === 0){ Quote Link to comment Share on other sites More sharing options...
Maq Posted March 16, 2009 Share Posted March 16, 2009 simpli, please use code tags next time... thx Quote Link to comment Share on other sites More sharing options...
simpli Posted March 16, 2009 Author Share Posted March 16, 2009 I dont understand the '===' operator. What does it do and why should i use it here.. Also what do you mean when you say to use the code tag? I dont know what it is so if I am being rude please accept appologies. Thank you for explaining etiquette. J-R Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 16, 2009 Share Posted March 16, 2009 The '===' means an identical comparison - not just evaluated equal. In other words, the values are just not "evaluated" as equal - they are exactly identical. A few examples should explain clearly: if (1 == true) //result is true (a 1 is evaluated as true) if (1 == 1) //result is true if (1 === true) //result is false (they are not the same type) if (1 === 1) //result is true This PHP manual page should help as well: http://us3.php.net/operators.comparison As for the code tags, you can simply select the PREVIEW button to get your post in a form where you can insert them with the available tools, or just put these around your code (without spaces) and it will display like my examples above: [ c o d e ]code goes here[ / c o d e ] 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.