Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > PHP Zip Code Validation ?

PHP Zip Code Validation ?
Thread Tools
Mac Elite
Join Date: May 2002
Location: Los Angeles, CA.
Status: Offline
Reply With Quote
Oct 16, 2003, 03:19 PM
 
// check zipcodes for validity

if (($ZIP_CODE) || ($zip_code)) {

$zip_code = trim($zip_code);

if ($ZIP_CODE)

$zip_code = trim($ZIP_CODE);

if (!ereg("(^[0-9]{5})-([0-9]{4}$)", trim($zip_code)) && (!ereg("^[a-zA-Z][0-9][a-zA-Z][[:space:]][0-9][a-zA-Z][0-9]$", trim($zip_code))) && (!ereg("(^[0-9]{5})", trim($zip_code)))) {

print_error("your <b>zip/postal code</b> is invalid");

}

}

I'm currently using this code, but I want to have ONLY five (5) digit zip codes work, and nothing else.

Can someone help me with modifiying this to work with ONLY five digits?

my PHP knowledge isn't up to par obviously

Thanks for any help!
     
Registered User
Join Date: Jul 2003
Location: San Jose
Status: Offline
Reply With Quote
Oct 16, 2003, 04:11 PM
 
this might be a little simpler... first checks to see if the input contains any letters, then checks the length...

[php]
<html>

<head>

</head>

<body>

<? if (!$_POST['submit']) { ?>

<form action="<? echo $PHP_SELF ?>" method="post" name="zipForm">
<input name="zipCode" type="text">
<input name="submit" type="submit">
</form>

<? } else {

// checks for letters, not case sensitive (ie a == A)
if (eregi("[a-z]", "$zipCode") == true)
{
echo "Zip codes do not contain letters.";
}
else
{
// counts how many characters were entered
if (strlen("$zipCode") != 5)
{
echo "A valid zip code must have 5 digits.";
}
else
{
echo "$zipCode is a valid zip code";
}
}

}

?>

</body>

</html>
[/php]
(Last edited by mzllr; Oct 16, 2003 at 05:28 PM. )
     
badtz  (op)
Mac Elite
Join Date: May 2002
Location: Los Angeles, CA.
Status: Offline
Reply With Quote
Oct 16, 2003, 06:56 PM
 
thanks
     
Dedicated MacNNer
Join Date: Aug 2002
Status: Offline
Reply With Quote
Oct 16, 2003, 07:00 PM
 
that example would allow symbols through, better safe then sorry:

[php]
<?
if ($zip_code || $ZIP_CODE) {
$zip_code = trim($zip_code);
$zip_code.= trim($ZIP_CODE);

if (!ereg("^[0-9]{5}$", trim($zip_code))) {

print_error("your zip/postal code is invalid");

}

}
?>
[/php]

the regular expression works as follows:
^ denotes the start
[0-9] means a number
{5} means there must be 5 repetitions of the previous []
& denotes the end

--will
     
Senior User
Join Date: Dec 2002
Status: Offline
Reply With Quote
Oct 17, 2003, 08:55 AM
 
Is there a good place to learn about ereg? I see it on the boards now and then but it always looks like so much gibberish. Well, I see how some of it works (0-9,a-z,etc), but don't know the specific syntax.
Travis Sanderson
     
Registered User
Join Date: Jul 2003
Location: San Jose
Status: Offline
Reply With Quote
Oct 17, 2003, 11:45 AM
 
thanks for catching the symbols, will. didn't even think about it...

i always turn to the trusty php manual when i need some help...
     
Senior User
Join Date: Dec 2002
Status: Offline
Reply With Quote
Oct 17, 2003, 12:26 PM
 
actually I meant regular expressions, sorry. I also frequent the php.net site
Travis Sanderson
     
Registered User
Join Date: Jul 2003
Location: San Jose
Status: Offline
Reply With Quote
Oct 17, 2003, 04:52 PM
 
ahhh gotcha... i searched google for 'regular expressions in php' and found this sitepoint article...

hope it's more along the lines of what you were looking for.
     
   
Thread Tools
Forum Links
Forum 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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 07:16 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2