n8dog
asked on
ZIP Code 5 Digit Value?
How do i validate a ZIP variable to ensure it's a 5 digit value?
-n
-n
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That prints 1 if $zip is 5 digits, prints 0 otherwise
It's
<?php
$zip="01234";
if (preg_match("/^\d{5}$/", $zip, $match) === 1)
echo 'Match';
else
echo 'No Match';
?>
<?php
$zip="01234";
if (preg_match("/^\d{5}$/", $zip, $match) === 1)
echo 'Match';
else
echo 'No Match';
?>
TeRReF, your solution is exactly what callrs said. What's the point in capturing the match when it's an all-or-nothing pattern?
ASKER
ended up using
$varlen = strlen($var)
thanks guys
In that case, make sure you also use ctype_digit($var) to check that $var contains only numeric characters.
ASKER
excellent.. thanks for that addition!
Yes, thanks! : )
A+
humeniuk, if you see this, please re-open question to allow HackneyCab a points share
(and this too if you can hehe http:Q_21982002.html)
A+
humeniuk, if you see this, please re-open question to allow HackneyCab a points share
(and this too if you can hehe http:Q_21982002.html)