JS form help :( cant complete project

Ouikikazz

Gawd
Joined
Feb 3, 2001
Messages
939
I got this web design course and my js sux, i got the form working and everything but i cant get the form to NOT allow a submit when the validation error comes up...basically when the fields are missing the alerts come up (thats fine) but still lets the form submit, can someone help me out and tell me how to fix my code...i need it to alert and NOT be able to submit

heres link to page and view source for code:

www.buffalo.edu/~lwong3/project7.html

thanks in advance for help
 
don't use a type=submit, use a type=button/image/something else innocuous. That way the HTML natively does nothing. Then, use JS to submit or fail as appropriate.
 
lomn75 said:
don't use a type=submit, use a type=button/image/something else innocuous. That way the HTML natively does nothing. Then, use JS to submit or fail as appropriate.

how do you use JS to submit or fail
 
Like the other guys were saying.. .have a button that goes to a JS function where you do your form validation. If the validation passes, call form.submit

I think on some browsers, you can also do a return(true)

Ouikikazz said:
I got this web design course and my js sux, i got the form working and everything but i cant get the form to NOT allow a submit when the validation error comes up...basically when the fields are missing the alerts come up (thats fine) but still lets the form submit, can someone help me out and tell me how to fix my code...i need it to alert and NOT be able to submit

heres link to page and view source for code:

www.buffalo.edu/~lwong3/project7.html

thanks in advance for help
 
Pretty easy to do.

Code:
  <script language="JavaScript">
  
  function OnSubmit_Validate(myForm) 
  {
    // Do validation
    if (valid)
  	return true;
    else
  	return false;
  }
  
  </script>
  
  <form onSubmit="return OnSubmit_Validate(this);">
  
  </form>

You can then just use a normal submit button.
 
SledgY said:
Pretty easy to do.

Code:
  <script language="JavaScript">
  
  function OnSubmit_Validate(myForm) 
  {
    // Do validation
    if (valid)
  	return true;
    else
  	return false;
  }
  
  </script>
  
  <form onSubmit="return OnSubmit_Validate(this);">
  
  </form>

You can then just use a normal submit button.

Exectly.
Your validation function will return "true" or "false" (you specify which), which sets the onSubmit Handler to True or False. If its set to false, the form won't submit.
 
Back
Top