simpli Posted March 17, 2009 Share Posted March 17, 2009 Hi, I've been trying to do a validation script for a form with textfields and radiobuttons. I've been having all sorts of problems as witnessed by my many postings. I admit at first I tried to do things too fast by just copy pasting. Since, I did the time to read some docs and further my knowledge. I have also cleaned my code (it was really messy) and decided to do a reboot and start with the field validations. Instead getelementbyid I am now using the document.form.object format. But still no luck. I have a very simple form with one text field that I want to validate. For some reason it doesn't work. I believe the function is called but that it doesnt get my field correctly because the validation doesnt run. Anyone can take a good look and tell me what I am doing wrong? I look at the code and I think it should be working but it's not. <script type='text/javascript'> function formValidator(){ // Make quick references to our fields var p1 = document.frmchoixronde1.player_1; // Validation of the text fields! if(notEmpty(p1, "Votre Joueur #1 n'a pas ete choisi")){ return true; } return false; } function Empty(elem, helperMsg){ if(elem.value.length == 0){ alert(helperMsg); elem.focus(); // set the focus to this input return false; } 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> <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><menu class="one">hello jeanru<p></menu> </select></fieldset></br></br><fieldset> <b><label for="player_1" style="width:2em">1</label></b><input name="player_1" id="player_1" 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...
friedemann_bach Posted March 17, 2009 Share Posted March 17, 2009 Replace "notEmpty(...)" by "!(Empty(...))" and it will produce the result you desire. BTW, always consult the error console while developing, this helps in most cases. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 17, 2009 Share Posted March 17, 2009 function formValidator(){ // Make quick references to our fields var p1 = document.frmchoixronde1.player_1; // Validation of the text fields! if(notEmpty(p1, "Votre Joueur #1 n'a pas ete choisi")){ return true; } return false; } function Empty(elem, helperMsg){ if(elem.value.length ===0){ alert(helperMsg); elem.focus(); // set the focus to this input return false; } 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; } } Quote Link to comment Share on other sites More sharing options...
simpli Posted March 17, 2009 Author Share Posted March 17, 2009 I'm on a mac and using MAMP. I dont know how to access the error console. Any clue? J-R Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 17, 2009 Share Posted March 17, 2009 i fixed some errors in the aboive code however i will error shoot it with MAMP Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 17, 2009 Share Posted March 17, 2009 do you have a link to the page? i could debug it Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 17, 2009 Share Posted March 17, 2009 Cleaned up more syntax was not valid and had output functions before defining them Code: function formValidator(){ // Make quick references to our fields var p1 = document.frmchoixronde1.player_1; function Empty(elem, helperMsg){ if(elem.value.length ===0){ alert(helperMsg); elem.focus(); // set the focus to this input return false; } // Validation of the text fields! if(!Empty(p1, "Votre Joueur #1 n'a pas ete choisi")){ return true; } return false; } 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; } } Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted March 17, 2009 Share Posted March 17, 2009 I'm on a mac and using MAMP. I dont know how to access the error console. Any clue? J-R You errors appear in your browser not your server. It doesn't matter what OS you have. Just get firefox and use the firebug plugin to debug easier Quote Link to comment Share on other sites More sharing options...
friedemann_bach Posted March 17, 2009 Share Posted March 17, 2009 Do not forget to empty the browser cache when testing. Quote Link to comment Share on other sites More sharing options...
Floydian Posted March 17, 2009 Share Posted March 17, 2009 I'm on a mac and using MAMP. I dont know how to access the error console. Any clue? J-R Safari also comes with a built in javascript console. I couldn't tell ya how to get to it, but it shouldn't be hard to do. http://www.jslint.com/ That web site can check your js code. It's owned/run by a Yahoo JavaScript Architect and inventor of JSON. This is a very recommended site. Quote Link to comment Share on other sites More sharing options...
simpli Posted March 17, 2009 Author Share Posted March 17, 2009 Guys thank you all, I was already on Firefox so i just fired up my console and were able to see my errors. It's much better that way. Most of them were corrected easily with the help of the console. Without the console it was a bit like flying blind. Especially since I was new to js. Thank you all very much. J-R Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 17, 2009 Share Posted March 17, 2009 don't forget to mark both your topics as solved then 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.