Last active
January 21, 2020 08:55
-
-
Save jefferyrdavis/5992282 to your computer and use it in GitHub Desktop.
Snippit: PHP: Simple US Zip Code format validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Validates if $zipCode is a 5 digit number in the 12345 format. Note that this simply checks to see if $zipCode is a 5 digit number, not necessarily a valid U.S. Zip Code. | |
*/ | |
function validateZipCode($zipCode) { | |
if (preg_match('#[0-9]{5}#', $zipCode)) | |
return true; | |
else | |
return false; | |
} | |
?> |
ghost
commented
May 8, 2018
•
tweaked a bit and it finally worked for me and includes the optional zip+4 validation if a user decides to include that
function isValidZipCode($zipCode) {
return (preg_match('/^[0-9]{5}(-[0-9]{4})?$/', $zipCode)) ? true : false;
}
check it out here (changed the return from bool to string in this demo): https://repl.it/repls/NativeCornyProject
what bout to add empty zipcode
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment