Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-06-2004, 03:15 AM   PM User | #1
tachyon
New Coder

 
Join Date: Jun 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
tachyon is an unknown quantity at this point
Question simple regular expressions question

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>
tachyon is offline   Reply With Quote
Old 07-06-2004, 03:47 AM   PM User | #2
Willy Duitt
Banned

 
Join Date: Sep 2003
Posts: 3,620
Thanks: 0
Thanked 0 Times in 0 Posts
Willy Duitt is an unknown quantity at this point
Try this one:

Code:
<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
Willy Duitt is offline   Reply With Quote
Old 07-06-2004, 03:55 AM   PM User | #3
tachyon
New Coder

 
Join Date: Jun 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
tachyon is an unknown quantity at this point
Thank you for your reply. This looks like some good code but I already have an onblur event on that field.

I tried this:
Code:
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.
tachyon is offline   Reply With Quote
Old 07-06-2004, 05:37 AM   PM User | #4
Willy Duitt
Banned

 
Join Date: Sep 2003
Posts: 3,620
Thanks: 0
Thanked 0 Times in 0 Posts
Willy Duitt is an unknown quantity at this point
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
Willy Duitt is offline   Reply With Quote
Old 07-06-2004, 03:41 PM   PM User | #5
tachyon
New Coder

 
Join Date: Jun 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
tachyon is an unknown quantity at this point
Oh, thanks. I didn't know about the decimals.
tachyon is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:44 PM.


Advertisement
Log in to turn off these ads.