|
it
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Second form validation problem
I have two forms on my page. Both use javascript to validate input before submission. Problem is that first form does the validation fine but the second form gets submitted without any validation taking place. Simplified form of code is given below. What am I doing wrong? Thanks Regards <html> <head> <script type="text/javascript"> // <![CDATA[ function validate() { // validation code here, which works fine } function valid1() { f=document.getElementById("Form1"); if(f.StaffCode.value<>"HS01") { alert("Invalied code. Please re-enter."); f.StaffCode.focus(); return false; } } // ]]> </script> </head> <body> <!--This form validates fine using function validate--> <form id="theform" method="post" action="abc.asp" onsubmit="return validate();"> <input type="hidden" name="mode" value="1"> <input type="text" name="muser" id="muser" value="" size="20"> <input type="password" name="mpass" id="mpass" value="" size="20"> <input name="bu_submit" type="image" src="images/box/bu_login_sm.gif" border="0" class="noborder" style="height: 31px;"> </form> <!--This form does not validate using function valid1--> <form id="Form1" method="post" action="xyz.asp" onsubmit="return valid1();"> <input type="text" name="StaffCode" id="StaffCode" value="" size="30"> <input name="bu_submit" type="image" src="images/box/bu_login.gif" border="0" class="noborder" style="height: 31px;"> </form> </body> </html> John wrote:
> Hi If you want to know what's wrong with your javascript, you should post> > I have two forms on my page. Both use javascript to validate input before > submission. Problem is that first form does the validation fine but the > second form gets submitted without any validation taking place. Simplified > form of code is given below. > > What am I doing wrong? > to a javascript group - microsoft.public.scripting.jscript, for example. But client side validation is not recommended. It does not work for those with javascript disabled, or those that want to get round it. And having acceptable log in values accessible to all through View Source rather defeats the whole point. You should move to server-side validation. -- Mike Brind Thanks. I just want a way to validate the second form and not necessarily
via javascript. Any help would be appreciated. The validation required is not fort Knox standard though, just to deter a casual user. Thanks Regards Show quote "Mike Brind" <paxton***@hotmail.com> wrote in message news:1152423700.600050.237590@75g2000cwc.googlegroups.com... > > John wrote: >> Hi >> >> I have two forms on my page. Both use javascript to validate input before >> submission. Problem is that first form does the validation fine but the >> second form gets submitted without any validation taking place. >> Simplified >> form of code is given below. >> >> What am I doing wrong? >> > > If you want to know what's wrong with your javascript, you should post > to a javascript group - microsoft.public.scripting.jscript, for > example. > > But client side validation is not recommended. It does not work for > those with javascript disabled, or those that want to get round it. > And having acceptable log in values accessible to all through View > Source rather defeats the whole point. You should move to server-side > validation. > > -- > Mike Brind > On Sun, 09 Jul 2006 00:28:53 -0500, John <John@nospam.infovis.co.uk> wrote:
> Hi Not tested, but this is most likely the problem.> > I have two forms on my page. Both use javascript to validate input before > submission. Problem is that first form does the validation fine but the > second form gets submitted without any validation taking place. > Simplified > form of code is given below. > > What am I doing wrong? > if(f.StaffCode.value<>"HS01") The inequality operator is != in Javascript. Also, your script will work more reliably if you use the DOM methods to access StaffCode directly rather than via its parent form. function valid1() { staffcode=document.getElementById("StaffCode"); if(staffcode.value!="HS01") { alert("Invalied code. Please re-enter."); staffcode.focus(); return false; } } |
|||||||||||||||||||||||