Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stop form with Javascript Alert

Status
Not open for further replies.

internetguy

Technical User
Nov 22, 2003
57
US
I have a javascript function that is called when clicking the submit buttion on a form, if there is information left out or if fields don't match it makes an alert notifying the user what they are missing. The only problem is that the page continues to be processed. I tried adding a return false; to try and stop it, but no go. Here's a basic idea for my if else statements

if(email.length == 0){
alert("You did not enter a valid email");
}

and so on.
 
The problem is most likely to do with your form declaration and the submit button code. This example shows one way to do what you want:
Code:
<script type="text/javascript">
function validate() {
  if(document.forms[0].email.length == 0) {
    alert("You did not enter a valid email");
    return false;
  }
  return true;
}
</script>
...
<form method="post" action="" onsubmit="return (validate())">
<input name="email" type="text"/>
<input type="submit" value="Send Form"/>
</form>

Hopefully you can take my example and build on it.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
You need to do two things:

1. Return false from your function is validation fails, true otherwise.

2. Add a return around the function call in the onsubmit handler:

Code:
<form onsubmit="return(myValidationFunc());">

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top