sam20e Posted November 11, 2014 Share Posted November 11, 2014 (edited) HelloI have a textbox and a input button which will insert a mysql record, however i need to validate that textbox if its empty it shouldnt allow that button to insert any record but show a popup error.my script : <script Language="JavaScript"> function Blank_TextField_Validator() { // Check the value of the element named text_name // from the form named text_form if (frmContact.id.value == "") { // If null display and alert box alert("Please fill in the text field."); // Place the cursor on the field for revision frmContact.id.focus(); // return false to stop further processing return (false); } // If text_name is not null continue processing return (true); } </script> Form : <form name="frmContact" id="frmContact" method="POST" action="index.php" onsubmit="return Blank_TextField_Validator()"> however its not validating or showing any error. any help? Edited November 11, 2014 by sam20e Quote Link to comment https://forums.phpfreaks.com/topic/292410-js-validation/ Share on other sites More sharing options...
Psycho Posted November 11, 2014 Share Posted November 11, 2014 OK, but you know you still need to add validation on the server-side too, right? Client-side validation is a great tool, but it should never be relied upon since it can be easily circumvented. In your code above, you are referencing "frmContact" in the function, but it is not defined. You can pass the form reference in the onsubmit call. Also, you should trim the value to ensure it doesn't pass with just spaces entered. <html> <head> <script> function Blank_TextField_Validator(formObj) { //Trim the value in the field formObj.id.value = formObj.id.value.trim(); // Check the value of the element named text_name // from the form named text_form if (formObj.id.value == "") { // If empty display and alert box alert("Please fill in the text field."); // Place the cursor on the field for revision formObj.id.focus(); // return false to stop further processing return false; } // If text_name is not null continue processing return true; } </script> </head> <body> <form name="frmContact" id="frmContact" method="POST" action="index.php" onsubmit="return Blank_TextField_Validator(this)"> <input type="text" name="id" id="id"> <button type="submit">Submit</button> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/292410-js-validation/#findComment-1496334 Share on other sites More sharing options...
sam20e Posted November 11, 2014 Author Share Posted November 11, 2014 Thank you for your prompt response. I adjusted my code but its still letting empty records to be saved. 1) I channged formObj to my form id 2) I changed id to another column/field name Apart from these 2 changes I copy/paste your new codes, so it should work right? am I making any errors? If you have any other JS scrtips to validate easily can you share with me? Quote Link to comment https://forums.phpfreaks.com/topic/292410-js-validation/#findComment-1496359 Share on other sites More sharing options...
Psycho Posted November 11, 2014 Share Posted November 11, 2014 Where is your current code? My code above works, so something in your current code is likely causing a JS error. Quote Link to comment https://forums.phpfreaks.com/topic/292410-js-validation/#findComment-1496361 Share on other sites More sharing options...
sam20e Posted November 12, 2014 Author Share Posted November 12, 2014 Where is your current code? My code above works, so something in your current code is likely causing a JS error. Hello, This is my update.php code : http://pastebin.com/RsqgRyhg Quote Link to comment https://forums.phpfreaks.com/topic/292410-js-validation/#findComment-1496372 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.