Postal Code ZIP Code Validation in PHP

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
  1. Austria => at
  2. Australia => au
  3. Canada => ca
  4. German => de
  5. Estonia => ee
  6. Netherlands => nl
  7. India => in
  8. Italy => it
  9. Portugal => pt
  10. Sweden => se
  11. United kingdom (England) => uk
  12. 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);
}


Related Topic How to Process XML Documents in Object Oriented Concepts
Biggest Hurdle of XML Schema
What is XML Schema Namespace Recommendation
XML Important Terminology in Object Oriented Programming
Struts FAQs

nScraps.com 2011   Privacy Policy  Terms of Service  Feedback