yandoo Posted April 4, 2008 Share Posted April 4, 2008 Hi there, i was hoping for a little help please ... I trying to create some java script validation on a text field so that it cannot be left empty... Heres what i got: <Script> function emptyvalidation(entered, alertbox) { // Emptyfield Validation by Henrik Petersen / NetKontoret // Explained at www.echoecho.com/jsforms.htm // Please do not remove this line and the two lines above. with (entered) { if (value==null || value=="") {if (alertbox!="") {alert(alertbox);} return false;} else {return true;} } } </script> </head> <body> <form id="form1" name="form1" method="post" action="tester2222" > <label> <input type="text" name="entered" onChange="emptyvalidation(this)"> </label> <label> <input type="submit" name="Submit" value="Submit" /> </label> </form> Trouble is it doesnt work at all??? I'm using this script direct from tutorial and cant get it working....What am i missing?? Thanks Quote Link to comment Share on other sites More sharing options...
rhodesa Posted April 4, 2008 Share Posted April 4, 2008 It's expecting text to alert. Change your input tag to this: <input type="text" name="entered" onChange="emptyvalidation(this,'You must enter a value')"> Quote Link to comment Share on other sites More sharing options...
yandoo Posted April 4, 2008 Author Share Posted April 4, 2008 Hi there, thanks for reply I just tested it and find it is still not working?? I can submit with empty records? What should i do? Thanks Quote Link to comment Share on other sites More sharing options...
rhodesa Posted April 4, 2008 Share Posted April 4, 2008 It's much better (in my opinion) to validate on submit anyways...try this out: <html> <head> <script type="text/javascript"> function validate(form){ if(!form.entered.value.length){ alert('Please enter a value'); form.entered.focus(); return false; } return true; } </script> </head> <body> <form id="form1" name="form1" method="post" action="tester2222" onsubmit="return validate(this);"> <label> <input type="text" name="entered" /> </label> <label> <input type="submit" name="Submit" value="Submit" /> </label> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
yandoo Posted April 4, 2008 Author Share Posted April 4, 2008 Hi, Yes i did originally try to validate by submit...I found the validation to work in that the message window appears says Required field...but after closing the little message window the record is updated anyway and the user is directed to another page...?????? I would actually prefer to do it by submit like you suggested but it just didnt stop the record being updated and page redirecting... Is that dooable?? Thanks Quote Link to comment Share on other sites More sharing options...
rhodesa Posted April 4, 2008 Share Posted April 4, 2008 The above shouldn't redirect. The key is to have the function return false, and also have a 'return' in front of the function call in the onsubmit. Try the code I posted, it should alert the message, move the cursor to the missing element, and NOT submit Quote Link to comment Share on other sites More sharing options...
yandoo Posted April 4, 2008 Author Share Posted April 4, 2008 Ok i will do that brill thank you.... Just one more thing if i may...would it be possible to change from just empty validation but to say a minimum of 3 characters has to be typed in the field?? thanks Quote Link to comment Share on other sites More sharing options...
yandoo Posted April 4, 2008 Author Share Posted April 4, 2008 Hi, I justed tested the script and get the same results?? Message box appears when click ok, record is inserted and page redirected still ???? whats going on?? Thanks Quote Link to comment Share on other sites More sharing options...
rhodesa Posted April 4, 2008 Share Posted April 4, 2008 yes you can...you can test something like form.fieldName.value.length == 3 well, my guess is you have an error in your javascript. if the javascript errors out and doesn't return that false, the form will still send. can you post the code you are actually using instead of the sample script? Quote Link to comment Share on other sites More sharing options...
yandoo Posted April 5, 2008 Author Share Posted April 5, 2008 <script type="text/javascript"> function validate(form){ if(!form.entered.value.length){ alert('Please enter a value'); form.entered.focus(); return false; } return true; } </script> </head> <body> <form id="form1" name="form1" method="post" action="tester2222.php" onsubmit="return validate(this);"> <label> <input type="text" name="entered" /> </label> <label> <input type="submit" name="Submit" value="Submit" /> </label> </form> </body> </html> That works.....as soon as i add an Update query then it redirects and updates record anyway! if add the validation to a text field in an existing form i have which updates records... then the vallidation is called, granted but it updates anyway???? All i'm using is an update query ($editformaction)..... Please help if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) { $updateSQL = sprintf("UPDATE animal SET BreedID=%s, Age=%s, Sex=%s, Image=%s, Neut=%s, Flea=%s, Worm=%s, Treatment=%s, VID=%s WHERE AnimalID=%s", GetSQLValueString($_POST['select'], "text"), GetSQLValueString($_POST['age'], "int"), GetSQLValueString($_POST['sex'], "text"), GetSQLValueString($_POST['image'], "text"), GetSQLValueString (isset($_POST['neut']) ? "true" : "", "defined","1","0"), GetSQLValueString(isset($_POST['flea']) ? "true" : "", "defined","1","0"), GetSQLValueString(isset($_POST['worm']) ? "true" : "", "defined","1","0"), GetSQLValueString(isset($_POST['treatment']) ? "true" : "", "defined","1","0"), GetSQLValueString($_POST['vet'], "text"), GetSQLValueString($_POST['AnimalID'], "int")); mysql_select_db($database_woodside, $woodside); $Result1 = mysql_query($updateSQL, $woodside) or die(mysql_error()); //Below change to add_animal2 $updateGoTo = "view_animal_details.php"; if (isset($_SERVER['QUERY_STRING'])) { $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?"; $updateGoTo .= $_SERVER['QUERY_STRING']; //$updateGoTo .= '&' . $_POST['AnimalID']; //}else{ $updateGoTo .= 'recordID=' . $_POST['AnimalID']; //above is new REMOVE } header(sprintf("Location: %s", $updateGoTo)); } Quote Link to comment Share on other sites More sharing options...
rhodesa Posted April 5, 2008 Share Posted April 5, 2008 Sorry, but that makes no sense. The PHP should not be interfering with the JavaScript Quote Link to comment Share on other sites More sharing options...
yandoo Posted April 7, 2008 Author Share Posted April 7, 2008 Hi there, Yes it doenst really make sense, but i'm telling the truth i promise I tested it out using a form like above and when i clicked submit (with empty field), the JS message popped up...when i clicked the close window it UPDATED or INSERTED the record anyway and then redirected to page.... (as commanded on either insert r update) I thought that it was wierd behavour....what the hell can it be??? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted April 7, 2008 Share Posted April 7, 2008 It's because you are using a form "like above". I am 99% sure you problem is that there is an error in your JS. Do this... 1) Add the following to the end of your validate() function right before the "return true": alert('Form Complete'); 2) Add the following code somewhere on the page inside the BODY (replace form1 with the value of your form's ID attribute) <input type="button" value="Form Test" onclick="validate(document.getElementById('form1'));" /> 3) Click the button. This will test the JavaScript without the form actually submitting. Look for any JavaScript errors. I am willing to bet money there is a JavaScript error. 4) Once you get rid of all your JavaScript errors, remove steps 1 & 2 and you're good to go. If that STILL doesn't work, you will have to post the ACTUAL code you are using to get any help. 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.