INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Validation

My form still submits even though I validate it. by HellTel
Posted: 27 Nov 02

A very common problem, this one.
On submission of a form, many people like to call a function to validate some data and then not submit the form if validation fails.  What most people tend to forget is to return the function called from the <form> tag.  If this is forgotten, the form will still submit.  So here is an example that will only submit if you enter the word "correct" into the text field.


<html>
<head>
<script language="javascript">
function validateData(){
  if (document.getElementById("txt").value != "correct"){
    alert('That is not correct, please try again');
    return false;  // if false is returned, the form action will not take place and the form won't submit
  } else {
    alert('That is correct, form will submit');
    return true;  // if true is returned, the form action will take place and the form has been submitted
  }
}
</script>
</head>

<body>
<form id="theForm" method="post" onSubmit="return validateData()" action="javascript:alert('It has submitted')">
<!-- Note the return in the onSubmit above, that is VERY important -->
<input id="txt" type="text"><br>
<input type="submit">
</form>
</body>

</html>

Back to Javascript FAQ Index
Back to Javascript Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close