I need to create a script that will validate all of the text boxes the user must fill out by ensuring that:

1. the user inputs doesn’t leave any boxes empty
2. the user inputs a valid zip code
3. the user inputs a valid a email address

<!-- Hide script from older browsers

		function isNum(passedVal) {
			if (passedVal == "") {
				return false
			}
			for (i=0; i<passedVal.length; i++) {
				if (passedVal.charAt(i) < "0") {
					return false
				}
				if (passedVal.charAt(i) > "9") {
					return false
				}
			}
			return true
		}

		function validZip(inZip) {
			if (inZip == "") {
				return true
			}
			if (isNum(inZip)) {
				return true
			}
			return false
		}

		function submitIt(carForm) {
			if (carForm.zip.value == "" && carForm.dealerList.selectedIndex == -1) {
				alert("You must either enter a Zip code, or pick the dealer closest to you")
				carForm.zip.focus()
				return false
			}
	
			if (!validZip(carForm.zip.value)) {
				alert("That is an invalid Zip code")
				carForm.zip.focus()
				carForm.zip.select()
				return false
			}
	
			return true
		}

		// End hiding script -->
	</SCRIPT>
<!-- Hide script from older browsers

		function validEmail(email) {
			invalidChars = " /:,;"
	
			if (email == "") {
				return false
			}
			for (i=0; i<invalidChars.length; i++) {
				badChar = invalidChars.charAt(i)
				if (email.indexOf(badChar,0) > -1) {
					return false
				}
			}
			atPos = email.indexOf("@",1)
			if (atPos == -1) {
				return false
			}
			if (email.indexOf("@",atPos+1) > -1) {
				return false
			}
			periodPos = email.indexOf(".",atPos)
			if (periodPos == -1) {
				return false
			}
			if (periodPos+3 > email.length)	{
				return false
			}
			return true
		}

		function submitIt(carForm) {
			if (!validEmail(carForm.emailAddr.value)) {
				alert("Invalid email address")
				carForm.emailAddr.focus()
				carForm.emailAddr.select()
				return false
			}
	
			return true
		}

		// End hiding script -->
	</SCRIPT>

Using the two code examples above how to I change it to fit into this page? -->

<html>
<head>
<title>Ask us about Skiing in Montana</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF">
<table cellspacing="0" cellpadding="0" border="0">
  <tr>
    <td width="4" height="10" valign="top"></td>
    <td width="61" height="10" valign="top"></td>
    <td width="37" height="10" valign="top"></td>
    <td width="25" height="10" valign="top"></td>
    <td width="12" height="10" valign="top"></td>
    <td width="490" height="10" valign="top"></td>
    <td width="9" height="10" valign="top"></td>
    <td width="68" height="10" valign="top"></td>
  </tr>
  <tr>
    <td width="4" height="30" valign="top"></td>
    <td width="61" height="30" valign="top"></td>
    <td width="37" height="30" valign="top"></td>
    <td width="25" height="30" valign="top"></td>
    <td width="12" height="30" valign="top"></td>
    <td width="490" height="112" rowspan="2" valign="top"> 
  <h1 align="center">Let us Tell you More about<br>
    Skiing in Montana</h1>
  </td>
    <td width="9" height="30" valign="top"></td>
    <td width="68" height="30" valign="top"></td>
  </tr>
  <tr>
    <td width="4" height="82" valign="top"></td>
    <td width="98" height="82" colspan="2" valign="top"><img src="backup.gif"></td>
    <td width="25" height="82" valign="top"></td>
    <td width="12" height="82" valign="top"></td>
    <td width="9" height="82" valign="top"></td>
    <td width="68" height="82" valign="top"></td>
  </tr>
  <tr>
    <td width="4" height="182" valign="top"></td>
    <td width="61" height="182" valign="top"></td>
    <td width="37" height="182" valign="top"></td>
    <td width="25" height="182" valign="top"></td>
    <td width="511" height="182" colspan="3" valign="top"> 
  <p><b>We'd love to tell you all about our great state and why thousands of skiers 
    keep coming back year.</b></p>
  <p><b> Call us at 1-800-Ski-MNTA<br>
    and we will answer any questions you have.</b></p>
  <p><b>You can also just fill out the form below and we will mail you a kit to 
    aid you in planning your next ski adventure.</b></p>
</td>
    <td width="68" height="182" valign="top"></td>
  </tr>
  <tr>
    <td width="4" height="146" valign="top"></td>
    <td width="61" height="146" valign="top"></td>
    <td width="641" height="146" colspan="6" valign="top"> 
  <form method="post" action="">
    <p><b>Full Name: </b>
<input type="text" name="textfield" size="30">
    &nbsp;
    </p>
    <p>
      <b>Street Address: </b>
<input type="text" name="textfield2" size="50">
    </p>
    <p><b>City:</b> 
      <input type="text" name="textfield3">
      <b>State:</b> 
      <input type="text" name="textfield4" size="3">
      <b>Zip Code: </b> 
      <input type="text" name="textfield5" size="10">
    </p>
    <p>&nbsp;
      <b> Email: </b> 
      <input type="text" name="textfield6" size="40">
    </p>
    <p> 
      <input type="submit" name="Submit" value="Submit">
      <input type="submit" name="Submit2" value="Reset">
    </p>
  </form>
</td>
  </tr>
  <tr>
    <td width="4" height="1" valign="top"><img width="4" height="1" src="transparent.gif"></td>
    <td width="61" height="1" valign="top"><img width="61" height="1" src="transparent.gif"></td>
    <td width="37" height="1" valign="top"><img width="37" height="1" src="transparent.gif"></td>
    <td width="25" height="1" valign="top"><img width="25" height="1" src="transparent.gif"></td>
    <td width="12" height="1" valign="top"><img width="12" height="1" src="transparent.gif"></td>
    <td width="490" height="1" valign="top"><img width="490" height="1" src="transparent.gif"></td>
    <td width="9" height="1" valign="top"><img width="9" height="1" src="transparent.gif"></td>
    <td width="68" height="1" valign="top"><img width="68" height="1" src="transparent.gif"></td>
  </tr>
</table>
</body>
</html>

And how to I make sure that users do not leave any text boxes empty?

Member Avatar for rajarajan2017

Sample:

<!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>Nicholas Gingerella Assignment 7</title>
</head>
<body>
<script language="javascript" type="text/javascript">
function validate_email()
{
  if (document.FORM.email.value==/^\w+@\w+\.com|net|edu$/)
  {;return true;}
  else if (document.FORM.email.value==/^\w+\.\w+@\w+\.com|net|edu$/)
  {;return true;}
  else 
  {alert("Not a valid email address");return false;}
}

function validate_fullname()
{
  if (document.FORM.fullname.value==/^[a-zA-Z]+\s[a-zA-Z]+$/)
  {;return true;}
  else 
  {alert("Invalid name format.");return false;}
}


function validate_zipcode()
{
  if (document.FORM.zipcode.value==/^[0-9]{5}$/|/^[0-9]{5}-[0-9]{4}$/)
  {;return true;}
  else 
  {alert("Invalid zipcode format.");return false;}
}

function validate_phonenumber()
{
  if (document.FORM.phone.value==/^[0-9]{3}-[0-9]{4}$/|/^[0-9]{3}-[0-9]{5}-[0-9]{4}$/)
  {;return true;}
  else 
  {alert("Not a valid phone number.");return false;}
}

function validate_form()
{
  if (validate_fullname()==false|validate_email()==false|validate_zipcode()==false|validate_phonenumber()==false)
    {return false;}
	else
	{return true;}
}
</script>
<form name="FORM"  onsubmit="return validate_form()">
Enter the requested information below:
<br />
First and last name:
<input type="text" name="fullname" />
<br />
Email Address:
<input type="text" name="email" />
<br />
Zip Code:
<input type="text" name="zipcode"  />
<br />
Phone number:
<input type="text" name="phone"  />
<br />
<input type="submit" value="Submit"/>
</form>
</body>
</html>

Hi EntangledDesi,

Did you ever figure this out? I have been trying to work on this for quite some time now. I keep getting nowhere. Could you point me in the right direction to help me out? It would be much appreciated.

Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.