php_guest Posted June 5, 2009 Share Posted June 5, 2009 <form action="complited.php" method="post"> <input type="text" name="test" /> <input type="text" name="test2" /> <input type="submit" /> </form> Please tell me how I can check on submit click if any text field is empty before it goes to complited.php with jquery. I want to check on click if any name is empty. If it is, than var checking=1 else continue with complited.php. Thank you Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted June 6, 2009 Share Posted June 6, 2009 Give the form an onsubmit function that checks whether all its fields are blank or not. Return true if all fields are *not* empty and false otherwise. It's a bit counter-intuitive there, but that's how it works. Quote Link to comment Share on other sites More sharing options...
php_guest Posted June 6, 2009 Author Share Posted June 6, 2009 so far I know I need to create function which will be executed onsubmit. But the problem I have is how to make if isGood = false { alert("Please fill all your data") before sending user to php file. Now it send user there in any way, either if isGood=false or true. <form action="complited.php" method="post" onsubmit="validate();"> <input type="text" name="test" id="test" /> <span id='testVal'></span> <input type="text" name="test2" id="test2" /> <span id='test2Val'></span> <input type="submit" /> </form> <script> function validate() { var isGood = true; //validate test if(document.getElementById('test').value=='') { isGood = false; document.getElementById('testVal').InnerHTML = 'required'; } //validate test2 if(document.getElementById('test2').value=="") { isGood = false; document.getElementById('test2Val').InnerHTML = 'required'; } //return true or false return isGood; } Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted June 6, 2009 Share Posted June 6, 2009 wrapping your code between [ code ] tags will make the code easier to read and select try it next time what happens if you add return to your onsubmit? <form action="complited.php" method="post" onsubmit="return validate();"> Quote Link to comment Share on other sites More sharing options...
php_guest Posted June 6, 2009 Author Share Posted June 6, 2009 tnx a million I made it like this and it works now! if (isGood== false){ alert (isGood); } return isGood; } 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.