The following validation checks if the string consists of all digits and if minimum length is 5.

$zipcode = '32421';

if (ctype_digit($zipcode) && (preg_match('/\d{5}/', $zipcode))) {

  echo $zipcode . ' is a valid Zip Code and has length 5.';

}

In the above code the function ctype_digit verifies if all character in the string are numeric and the preg_match function checks for the required minimum length. Replace 5 with whatever value you would like.

Examples
$zipcode = 'ewr34';
output: FALSE

$zipcode = '43534535';
output "43534535 is a valid Zip Code and has length 5"

$zipcode = '12345';
output "12345 is a valid Zip Code and has length 5"
Tagged with:
 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>