• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

JavaScript form validation

I have a trouble with validation my form.  My problems are as follows:
1.  It doesn't show the error messages if I skip input fields entries.
2.  It submits without showing error message.

In order to validate each entries and validate the form when I click the submit button, what am I missing...?
I understand that there are various ways to validate forms, but if someone can fix my form and make it work, it would be wonderful.

I'm still new to JavaScript and probably I'm missing something here and there...  I'm following some textbooks but it's still difficult for me to learn... :(

Thank you for your help in advance.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Flowers for your loved one delivery form</title>
<link href="form.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">

function validateThis(formObj)
{
	var returnValue = true;
	
	if (formObj["zip"].value.length !==5 ||  formObj["zip"].value.length == 0)
	{
		document.getElementById("err_msg").innerHTML = "Please enter your 5 digit zip code.";
		returnValue = false;
	}
	 else {
		 if (isNaN(formObj["zip"].value)) || formObj["zip"].value.length == 0)
		 {
			 document.getElementById("err_msg".innerHTML = "Please enter numbers.";
									 returnValue = false;
		 }
	 }
	 else {
		 if (formObj["state"].value.length == 0)
		 {
			 document.getElementById("err_msg").innerHTML = "Please enter your state.";
			 returnValue = false;
		 }
	 }
	 else {
		 if (formObj["city"].value.length == 0)
		 {
			 document.getElementById("err_msg").innerHTML = "Please enter your city.";
			 returnValue = false;
		 }
	 }
	 else {
		 if (formObj["street"].value.length == 0)
		 {
			 document.getElementById("err_msg").innerHTML = "Please enter your street.";
			 returnValue = false;
		 }
	 }
	 else {
		 if (formObj["name"].value.length == 0)
		 {
			 document.getElementById("err_msg").innerHTML = "Please enter your name.";
			 returnValue = false;
		 }
		 return returnValue;
	 }
}

</script>
</head>

<body onload="document.getElementById('message').focus();document.getElementById('message').select(); err_msg.innerHTML ='';">
<div class = "container">
<div id="header"><img src="images/free-rain-flowers-vector-graphic_73789_small.jpg" width="237" height="150" alt="flowers" />

<h1>&nbsp;</h1>
<h1>Flowers for your loved one</h1>
</div>

<h2>&nbsp;</h2>
<h2>Thank you for your order!  Please let us know the shipping address below.</h2>
<br />
<form id="delivery" name="delivery" method="post" action="form.php">
  <table width="880" cellpadding="8px">
 <div class="field">
 <span id="err_msg" class="help"></span> <br />
  <tr>
  <td>
    <div class = "field">
    <h3>Name:</h3>
      <input type="text" name="name" id="name" size="25"  />
    </div>
    
    <div class = "field">
    <h3>Street:</h3>  
      <input type="text" name="street" id="street" size="50"  />
      </div>
    
    <div class = "field">
    <h3>City:</h3>
      <input type="text" name="city" id="city" size="15"  />
    </div>
      
    <div class = "field">
    <h3>State:</h3>
      <input type="text" name="state" id="state" size="2"  />
    </div>
    
    <div class = "field">
    <h3>Zip code:</h3>
      <input type="text" name="zipcode" id="zipcode" size="5"  />
    </div>   
    <br />
    <input name="submit" type="submit" id="button" onClick="return validateThis(this.form);" value="Submit" />
    
  </td>
  </tr>
  </table>
</form>
</div>
</body>
</html>

Open in new window

0
sabregirl
Asked:
sabregirl
3 Solutions
 
chaitu chaituCommented:
<form id="delivery" name="delivery" method="post" action="form.php" onsubmit="return validateThis(this.form);" >

<input name="submit" type="submit" id="button"  value="Submit" />
0
 
sonawanekiranCommented:
Check this code (TESTED)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Flowers for your loved one delivery form</title>
<link href="form.css" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

function validateThis(){
  var returnValue = true;
  
  if ($("#zipcode").val().length !== 5 || $("#zipcode").val().length == 0) {
    document.getElementById("err_msg").innerHTML = "Please enter your 5 digit zip code.";
    returnValue = false;
  }
  else 
    if (isNaN($("#zipcode").val()) || $("#zipcode").val().length == 0) {
      document.getElementById("err_msg").innerHTML = "Please enter numbers.";
      returnValue = false;
    }
    
    else 
      if ($("#state").val().length == 0) {
        document.getElementById("err_msg").innerHTML = "Please enter your state.";
        returnValue = false;
      }
      else 
        if ($("#city").val().length == 0) {
          document.getElementById("err_msg").innerHTML = "Please enter your city.";
          returnValue = false;
        }
        
        else 
          if ($("#street").val().length == 0) {
            document.getElementById("err_msg").innerHTML = "Please enter your street.";
            returnValue = false;
          }
          
          else 
            if ($("#name").val().length == 0) {
              document.getElementById("err_msg").innerHTML = "Please enter your name.";
              returnValue = false;
            }
  
  
  
  return returnValue;
}

</script>
</head>

<body>
<div class = "container">
<div id="header"><img src="images/free-rain-flowers-vector-graphic_73789_small.jpg" width="237" height="150" alt="flowers" />

<h1>&nbsp;</h1>
<h1>Flowers for your loved one</h1>
</div>

<h2>&nbsp;</h2>
<h2>Thank you for your order!  Please let us know the shipping address below.</h2>
<br />
<form id="delivery" name="delivery" method="post" action="form.php">
  <table width="880" cellpadding="8px">
 <div class="field">
 <span id="err_msg" class="help"></span> <br />
  <tr>
  <td>
    <div class = "field">
    <h3>Name:</h3>
      <input type="text" name="name" id="name" size="25"  />
    </div>
    
    <div class = "field">
    <h3>Street:</h3>  
      <input type="text" name="street" id="street" size="50"  />
      </div>
    
    <div class = "field">
    <h3>City:</h3>
      <input type="text" name="city" id="city" size="15"  />
    </div>
      
    <div class = "field">
    <h3>State:</h3>
      <input type="text" name="state" id="state" size="2"  />
    </div>
    
    <div class = "field">
    <h3>Zip code:</h3>
      <input type="text" name="zipcode" id="zipcode" size="5"  />
    </div>   
    <br />
    <input name="submit" type="submit" id="button" onClick="return validateThis();" value="Submit" />
    
  </td>
  </tr>
  </table>
</form>
</div>
</body>
</html>

Open in new window

0
 
sabregirlAuthor Commented:
Thank you for all of your comments.  My main computer died and I didn't have time to access to the site.  I found my problem, which was in this line...

if (isNaN(formObj["zip"].value)) || formObj["zip"].value.length == 0)

I had an extra parenthesis and that caused the trouble.  I understand the differences between onclick and onsubmit, but in this case, both actually worked and it wasn't my issue.

I appreciate all of your help.  Debugging is not easy, I have to say...
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now