PHP code validation for postal code / Zip Code validation
This below PHP code will help to validate the PIN code numbers / ZIP code in country basis. This code will support for the below list of countries
- Austria => at
- Australia => au
- Canada => ca
- German => de
- Estonia => ee
- Netherlands => nl
- India => in
- Italy => it
- Portugal => pt
- Sweden => se
- United kingdom (England) => uk
- United States of America => us
Just page the postal code and the country code like us, uk listed in above list. This program will return true if the postal code is a validate one else it will return false.
function validate_postalcode($val, $country){
$patterns = array('at' => '^[0-9]{4,4}$', 'au' => '^[2-9][0-9]{2,3}$', 'ca' =>
'^[a-zA-Z].[0-9].[a-zA-Z].s[0-9].[a-zA-Z].[0-9].', 'de' => '^[0-9]{5,5}$', 'ee' =>
'^[0-9]{5,5}$', 'nl' => '^[0-9]{4,4}s[a-zA-Z]{2,2}$', 'in' => '^[0-9]{6,6}$', 'it' => '^[0-9]{5,5}$',
'pt' => '^[0-9]{4,4}-[0-9]{3,3}$', 'se' => '^[0-9]{3,3}s[0-9]{2,2}$', 'uk' =>
'^([A-Z]{1,2}[0-9]{1}[0-9A-Z]{0,1}) ?([0-9]{1}[A-Z]{1,2})$', 'us' =>
'^[0-9]{5,5}[-]{0,1}[0-9]{4,4}$');
if (!array_key_exists($country, $patterns))
return false;
return (bool)preg_match("/" . $patterns[$country] . "/", $val);
}
