/    Sign up×
Community /Pin to ProfileBookmark

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>[/CODE]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@fredmvJul 06.2004 — if(/(^d{5}$)|(^d{5}-d{4}$)/.test(document.forms[0].elements[3].value))
Copy linkTweet thisAlerts:
@tachyonauthorJul 06.2004 — Thank you for the reply. However, when I substitute that line of code and run it, it accepts "a" as a zip code with no complaints.

Is there something I could change to make it function better?
Copy linkTweet thisAlerts:
@fredmvJul 06.2004 — [i]Originally posted by tachyon [/i]

[B]However, when I substitute that line of code and run it, it accepts "a" as a zip code with no complaints.[/B][/QUOTE]
It shouldn't. Type this in location bar:javascript&amp;#58;/(^d{5}$)|(^d{5}-d{4}$)/.test('a');You'll get the string "false", i.e., it didn't accept it. Really though, for a more streamlined form validation script you should seriously consider checking out Peter Bailey's [url=http://www.peterbailey.net/fValidate]fValidate[/url].
Copy linkTweet thisAlerts:
@tachyonauthorJul 06.2004 — if(!/(^d{5}$)|(^d{5}-d{4}$)/.test(document.forms[0].elements[3].value))

I put a "!" in front of the regular expression and it works. Thanks for the help! I'm not being sarcastic, either. I appreciate your helping me out.
Copy linkTweet thisAlerts:
@fredmvJul 06.2004 — Oh, right. I'm looking at this out of context. The logical NOT operator is definitely what you wanted. This also explains why it accepted "a" earlier: the test didn't succeed, hence, it didn't execute that block of code. Sorry about that. In any case, good to see you got it working.
×

Success!

Help @tachyon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...