Link to home
Start Free TrialLog in
Avatar of sabregirl
sabregirlFlag for United States of America

asked on

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

SOLUTION
Avatar of chaitu chaitu
chaitu chaitu
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Kiran Sonawane
Kiran Sonawane
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sabregirl

ASKER

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...