Link to home
Start Free TrialLog in
Avatar of allinc666
allinc666

asked on

JavaScript validation with an onChange in a form.

Hi I have a quick question. How do I make a Javascript code that checks to see if 9 numbers are entered in to a text field then takes you to another page while passing the 9 numbers into a form?
Avatar of CJ_S
CJ_S
Flag of Netherlands image

function formSubmit(sVal)
{
   var sError = "";
   if(sVal.length!=9 || isNaN(sVal)) sError+="- Please enter 9 digits";

   if(sError!="") alert("The following errors occured: \n"+sError);
   return sError=="";
}

and call it using
<form>
<input type="text" onChange="if(formSubmit(this.value)) this.form.submit();">
</form>
<html>
 <head>
  <script language="javascript">
function formSubmit(sVal)
{
  var sError = "";
  if(sVal.length!=9 || isNaN(sVal)) sError+="- Please enter 9 digits";

  if(sError!="") alert("The following errors occured: \n"+sError);
  return sError=="";
}
  </script>
 </head>

 <body>
  <form action="nextpage.asp">
   <input type="text" onChange="if(formSubmit(this.value)) this.form.submit();">
  </form>
 </body>
</html>


or with the form being:

  <form action="nextpage.asp" onSubmit="return formSubmit(this.txt1.value)">
   <input type="text" name="txt1"><br>
   <input type="submit">
  </form>
ASKER CERTIFIED SOLUTION
Avatar of cubrovic
cubrovic
Flag of Serbia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jaysolomon
jaysolomon

No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: cubrovic {http:#8759027} & CJ_S {http:#8759133}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jAy
EE Cleanup Volunteer