simpli Posted March 14, 2009 Share Posted March 14, 2009 Hi all, and thanks in advance for your help. I have this javascript validation script to validate if a field is empty and it doesnt seem to work. It enters the script (I know because the hello i placed to trace shows) but then it just sits there and I dont know what is the cause of this. I'm expecting an error message when the field is empty and I'm just not getting it. This is my first attempt at js so bear with me and thank you in advance, J-R Can you help? The code is below. <script type='text/javascript'> function formValidator(){ // Make quick references to our fields document.write("Hello"); var p1 = document.getElementById('player_1'); document.write(p1.value); // Check each input in the order that it appears in the form! if(notEmpty(p1, "Please enter something, anything please!")){ 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; } </script> <?php /*Form header information */ print <<<htmldebut <form id="frmchoixronde1" name="frmchoixronde1" method="POST" enctype="application/x-www-form-urlencoded" onsubmit='return formValidator()'> <b>type something</b> htmldebut; 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'])){ $nbjoueurs = 1; $error=''; //initialize $error to blank for ($i=1; $i <= $nbjoueurs; $i++) { if(trim($_POST['player_' . $i])=='') { $error .="Votre joueur #" . $i . " n'a pas ete choisi!<br />"; } } //There are no errors we can write to database if($error == ''){ echo 'no errors'; }else{ echo '<fieldset style="width:450px">'; echo '<legend>Erreurs</legend>'; echo $error; echo '</fieldset>'; } } ?> <?php echo '<br/>'; echo '<fieldset>'; echo '<b><label for="player_1" style="width:2em"> 1. </label></b><input name="player_1" id="player_1" type="text" size="30"><br/>'; echo '</fieldset>'; echo '<input type="submit" name="submit" value="Soumettre vos choix">'; //End of the form ?> </form> <br/> </body></html> Quote Link to comment Share on other sites More sharing options...
hastishah Posted March 14, 2009 Share Posted March 14, 2009 Hi test this code its working little bit changes are done.. <script type='text/javascript'> function formValidator(){ // Make quick references to our fields var p1 = document.getElementById('player_1'); // Check each input in the order that it appears in the form! if(notEmpty(p1, "Please enter something, anything please!")){ 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; } </script> <?php /*Form header information */ print <<<htmldebut <form id="frmchoixronde1" name="frmchoixronde1" method="POST" enctype="application/x-www-form-urlencoded" onsubmit='return formValidator()'> <b>type something</b> htmldebut; 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'])){ $nbjoueurs = 1; $error=''; //initialize $error to blank for ($i=1; $i <= $nbjoueurs; $i++) { if(trim($_POST['player_' . $i])=='') { $error .="Votre joueur #" . $i . " n'a pas ete choisi!<br />"; } } //There are no errors we can write to database if($error == ''){ echo 'no errors'; }else{ echo '<fieldset style="width:450px">'; echo '<legend>Erreurs</legend>'; echo $error; echo '</fieldset>'; } } ?> <?php echo '<br/>'; echo '<fieldset>'; echo '<b><label for="player_1" style="width:2em"> 1. </label></b><input name="player_1" id="player_1" type="text" size="30"><br/>'; echo '</fieldset>'; echo '<input type="submit" name="submit" value="Soumettre vos choix">'; //End of the form ?> </form> <br/> </body></html> Quote Link to comment Share on other sites More sharing options...
simpli Posted March 14, 2009 Author Share Posted March 14, 2009 Hi, can you tell me what changes were made and why? I've been struggling to get it to work and I'd like to know what my mistakes were so I can learn from them. Since i dont have a version management system and I can't see the changes you made can you tell me what they were? Thank you very much. J-R Quote Link to comment Share on other sites More sharing options...
hastishah Posted March 14, 2009 Share Posted March 14, 2009 hi the changes which i made you can see them in code commented <script type='text/javascript'> function formValidator(){ // Make quick references to our fields // document.write("Hello"); /* comment this code to run your code because When you write on document it will clear old document so it will not able to get the document.getElementById('player_1');*/ var p1 = document.getElementById('player_1'); // document.write(p1.value); // Check each input in the order that it appears in the form! if(notEmpty(p1, "Please enter something, anything please!")){ 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; } </script> <?php /*Form header information */ print <<<htmldebut <form id="frmchoixronde1" name="frmchoixronde1" method="POST" enctype="application/x-www-form-urlencoded" onsubmit='return formValidator()'> <b>type something</b> htmldebut; 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'])){ $nbjoueurs = 1; $error=''; //initialize $error to blank for ($i=1; $i <= $nbjoueurs; $i++) { if(trim($_POST['player_' . $i])=='') { $error .="Votre joueur #" . $i . " n'a pas ete choisi!<br />"; } } //There are no errors we can write to database if($error == ''){ echo 'no errors'; }else{ echo '<fieldset style="width:450px">'; echo '<legend>Erreurs</legend>'; echo $error; echo '</fieldset>'; } } ?> <?php echo '<br/>'; echo '<fieldset>'; echo '<b><label for="player_1" style="width:2em"> 1. </label></b><input name="player_1" id="player_1" type="text" size="30"><br/>'; echo '</fieldset>'; echo '<input type="submit" name="submit" value="Soumettre vos choix">'; //End of the form ?> </form> <br/> </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.