Hello,

Can someone help me with the below javascript?

When a user enter zip code and if zip code >= 980 && ZIP <= 994 then it should auto fill state field as "WA".

Please let me know how to fix this?

Many Thanks


<html>

<head>

<title>Assignment 6</title>

<script type="text/javascript" language="javascript">

// when we load document, reset the form and prepare it for input
// by calling resetForm() function.
// document.forms[0] means "the first form in the document"
// document.forms[1] would mean "the second form in the document", etc.


window.onload = loadDoc;

function loadDoc() {
resetForm(document.forms[0]);
}
function isNum(passedVal) {
if (passedVal == "") {
return false;
}
for (var k=0; k<passedVal.length; k++) {
if (passedVal.charAt(k) < "0") {
return false;
}
if (passedVal.charAt(k) > "9") {
return false;
}
}
return true;
}









function isZip(inZip) {
if (inZip == "") {
return true;
}
return (isNum(inZip));
}



function validEmail(email)

{
var invalidChars = " /:,;";

if (email == "") {
return false;
}
for (var k=0; k<invalidChars.length; k++) {
var badChar = invalidChars.charAt(k);
if (email.indexOf(badChar) > -1) {
return false;
}
}
var atPos = email.indexOf("@",1);
if (atPos == -1) {
return false;
}
if (email.indexOf("@",atPos+1) != -1) {
return false;
}
var periodPos = email.indexOf(".",atPos);
if (periodPos == -1) {
return false;
}
if (periodPos+3 > email.length) {
return false;
}
return true;
}

function invalidLabel(parentTag) {
if (parentTag.nodeName == "LABEL") {
parentTag.className += " invalid";
}
}





function submitIt(inputForm) {

// 'inputForm' is now equal to the form object
// because we used the 'this' keyword when we
// called the function via the 'onsubmit" event
// handler in the HTML below


// begin by clearing out the error display area
// so that we start with a clean slate:
document.getElementById("formerrors").innerHTML=" "


// if name is blank, make user go back and correct
// it before continuing on:








if (validEmail(inputForm.email.value)== false || inputForm.email.value == "")
{

document.getElementById("formerrors").innerHTML = "You must enter a valid email address";

inputForm.email.focus();
inputForm.email.select();
return false;
}


if (inputForm.username.value == "") {
document.getElementById("formerrors").innerHTML = "You must enter your name";
inputForm.username.focus();
inputForm.username.select();
return false;
}

if (inputForm.Address.value == "") {
document.getElementById("formerrors").innerHTML = "You must enter your Address";
inputForm.Address.focus();
inputForm.Address.select();
return false;
}
if (inputForm.city.value == "") {
document.getElementById("formerrors").innerHTML = "You must enter your City";
inputForm.city.focus();
inputForm.city.select();
return false;
}
if (inputForm.zip.value == "") {
document.getElementById("formerrors").innerHTML = "You must enter your Zip";
inputForm.zip.focus();
inputForm.zip.select();
return false;
}


var ZIP = inputForm.zip.value;
ZIP = Number(ZIP.substring(0,3));
if (ZIP >= 980 && ZIP <= 994)
{
inputForm.state.value = "WA"
document.getElementById("statename").innerHTML = "You are from WA state";

}





if (inputForm.state.value == "") {
document.getElementById("formerrors").innerHTML = "You must enter your State";
inputForm.state.focus();
inputForm.state.select();
return false;
}
// if we made it to here, the form is OK!
// now we want to send output to a new window:

newWindow = window.open("","newWin");
newWindow.focus();
newWindow.document.write("<p><strong>Email: </strong> <em>" +
inputForm.email.value.toLowerCase() +
"</em></p>");
newWindow.document.write("<p><strong>Your Name: </strong> <em>" +
inputForm.username.value.toUpperCase() +
"</em></p>");
newWindow.document.write("<p><strong>Address: </strong> <em>" +
inputForm.address.value.toUpperCase() +
"</em></p>");
newWindow.document.write("<p><strong>City: </strong> <em>" +
inputForm.city.value.toUpperCase() +

"</em></p>");
newWindow.document.write("<p><strong>Zip: </strong> <em>" +
inputForm.zip.value.toUpperCase() +
"</em></p>");

newWindow.document.write("<p><strong>State: </strong> <em>" +
inputForm.state.value.toUpperCase() +
"</em></p>");

var content = newWindow.document.getElementById('email').innerHTML;
newWindow.document.getElementById('email').innerHTML= content + email;



newWindow.document.close();

return false;



}

function resetForm(inputForm) {

// set the value of the 'Email' variable to blank
inputForm.email.value="";
inputForm.username.value="";
inputForm.Address.value="";
inputForm.city.value="";
inputForm.zip.value="";
inputForm.state.value="";

// focus on first form field
inputForm.email.focus();

}



</script>

</head>

<body>

<h3>Javascript Sample Form</h3>

<form method="post" name="myform" onsubmit="return submitIt(this);">
<p><label for="email">Email Address:
<input id="email" type="text" size="30" class="reqd email" />
</label></p>

<p><label for="username">Name:</label>
<input type="text" id="username" name="username" /></p>
<p><label for="address">Address:</label>
<input type="text" size = "50" id="Address" name="address" /></p>
<p><label for="city">City:</label>
<input type="text" id="city" name="city" /></p>


<p><label for="zip">Zip:
<input id="zip" type="text" size="5" maxlength="5" /></p>

<p><label for="state">State:</label>
<input type="text" id="state" name="state" /></p>

</label></p>



<div style="color:red;font-weight:bold">
<span id="formerrors"> </span><br /><br />
</div>
<div style="color:green;font-weight:bold">
<span id="statename"> </span><br /><br />
</div>

<input type="submit" value="Submit" />


<input type="button" value="Reset" onclick="resetForm(this.form);" />
<!-- "this.form" above means "the form that this input element is on" -->

</form>

</body>
</html>