...

View Full Version : simple regular expressions question



tachyon
07-06-2004, 04:15 AM
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?


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

Willy Duitt
07-06-2004, 04:47 AM
Try this one:



<script type="text/javascript">
function chkZip(input){
if(!input.match(/^[0-9]{5}([- /]?[0-9]{4})?$/)){
alert ("please correct your zip code!");
return false;
}
}
</script>
</head>

<body>
<form>
<input type="text" onblur="chkZip(this.value)">
</form>


.....Willy

tachyon
07-06-2004, 04:55 AM
Thank you for your reply. This looks like some good code but I already have an onblur event on that field.

I tried this:

if(!/(^\d{5}$)|(^\d{5}-\d{4}$)/.test(document.forms[0].elements[3].value))
{
alert ("please correct your zip code!");
return false;
}

And it worked.

Willy Duitt
07-06-2004, 06:37 AM
Watch using \d as it will allow decimal points/periods...
BTW: I was not implying that you needed to use onblur (although I would), it was only provided as a working example....

......Willy

tachyon
07-06-2004, 04:41 PM
Oh, thanks. I didn't know about the decimals.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum