r-it Posted February 21, 2007 Share Posted February 21, 2007 can someone please help, i have this script: <script language="javascript"> function valid() { var input = document.fmRetail.txName.value; if(input == "") { alert("Please enter a retail product name before continuing"); fmRetail.txName.focus(); return false; } else { document.fmRetail.txName.focus(); return true; } } </script> which validates if a textbox is empty when one of the submit buttons is clicked, but the problem is that it shows the alert box, but submits the form regardless, what could be the cause of this?Here is the syntax for the form onsubmit: $show = "<form action=\"$_SERVER[php_SELF]\" method=\"post\" name=\"fmRetail\" onSubmit='return valid()' > and the focus() doesnt seem to work for anything, am I doing it wrong or something Quote Link to comment Share on other sites More sharing options...
nloding Posted February 21, 2007 Share Posted February 21, 2007 Unless you've defined it elsewhere ... focus() is not a javascript function. There is the function "onFocus()", companion to "onBlur()", which is for when someone clicks on the text area. I did a quick google search for focus() and found these: http://javascript.internet.com/forms/form-focus.html http://lists.evolt.org/archive/Week-of-Mon-20021125/129068.html Which should give you an idea of how to build the function if you haven't already. As to why the alert box always pops up, I have no idea. Try this: var textbox = document.fmRetail.txName; if(textbox.value == '') { I've always had bad luck defining a value in one line ... no idea why. If that wasn't the issue, then explain it a little different. Also, in the if statement, it would be document.fmRetail.txName.focus(); Quote Link to comment Share on other sites More sharing options...
r-it Posted February 21, 2007 Author Share Posted February 21, 2007 its cool i got it fixed <script language="javascript"> function valid(f) { var input = f.txName.value; if(input == "") { alert("Please enter a retail product name before continuing"); f.txName.focus(); return false; } else { f.txName.focus(); return true; } } </script> and the form's 1st line <form action=\"$_SERVER[php_SELF]\" method=\"post\" name=\"fmRetail\" onSubmit='return valid(fmRetail)' > and it works perfectly, thanks neway nloding 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.