 |
 |
PHP Zip Code Validation ?
|
 |
|
 |
|
Mac Elite
Join Date: May 2002
Location: Los Angeles, CA.
Status:
Offline
|
|
// 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
|
|
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.
)
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: May 2002
Location: Los Angeles, CA.
Status:
Offline
|
|
thanks 
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Aug 2002
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|