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"
Categories
- Ecommerce (1)
- PHP (1)
- Python (1)
- Uncategorized (1)
- WordPress (2)
Planet PHP- PHP Subconference at FrOSCon 2013 May 25, 2013 Kore Nordmann
- XPath expert needed May 24, 2013 Lukas Smith
- Phix 0.16.0 Released May 23, 2013 Stuart Herbert
- UA Testing with Selenium and PHPUnit May 23, 2013 Michelangelo van Dam
- Simple Mysql PHP Menu May 23, 2013 Kevin Waterson





