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

sabregirlAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Kiran SonawaneProject LeadCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.