Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple regular expression question

Status
Not open for further replies.

tachyon80

Technical User
Joined
Jun 19, 2004
Messages
13
Location
US
I'm having a problem making the regular expression for U.S. zip code verification work.
the regular expression: /(^\d{5}$)|(^\d{5}-\d{4}$)/

My code is below. No matter what I type in, it asks me to correct my zip code. Any suggestions?

Code:
<html>
<head>
	<title>U.S. Address</title>
<script language="Javascript" type="text/javascript">
<!-- hide script from old browsers
function validateForm()
{
if (document.forms[0].elements[0].value == "")
	{
	alert ("please enter an address!");
	return false;
	}
if (document.forms[0].elements[1].value == "")
	{
	alert ("please enter a city!");
	return false;
	}
if (document.forms[0].elements[2].value == "--")
	{
	alert ("please select a state!");
	return false;
	}
if (document.forms[0].elements[3].value == "")
	{
	alert ("please enter a zip code!");
	return false;
	}
if (document.forms[0].elements[3].value = "/(^\d{5}$)|(^\d{5}-\d{4}$)/")
	{
	alert ("please correct your zip code!");
	return false;
	}

else {
window.close();
updateParent5(this)
}
}

function updateParent1(textField){
opener.document.contactinfo.address.value=textField.value
}
function updateParent2(textField){
opener.document.contactinfo.city.value=textField.value
}
function updateParent3(textField){
opener.document.contactinfo.state.value=textField.value
}
function updateParent4(textField){
opener.document.contactinfo.zip.value=textField.value
}
function updateParent5(textField){
opener.document.contactinfo.country.value='USA'
}
// end hiding -->
</script>
</head>

<body>
<form action="us-add.php" onsubmit="return validateForm()">
address: <input type="text" onblur="updateParent1(this)"><br>
city: <input type="text" onblur="updateParent2(this)"><br>
state: <select onblur="updateParent3(this)">
	<option value="--">select</option>
<!-- lots of U.S. states here -->
</select>
 zip: <input type="text" name="zip1" onblur="updateParent4(this)"><br>
country: USA<br><br>
<input type="submit" name="submit">
</form>

</body>
</html>
 
/(^\d{5}-?(\d{4})?$)/

does that work???

Known is handfull, Unknown is worldfull
 
I'll use that expression. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top