#native_company# #native_desc#
#native_cta#

Postal Code Validator!

By Rudy Rockstar
on August 13, 2002

Version: 1.0

Type: Function

Category: Algorithms

License: GNU General Public License

Description: Validates postal code based on country (currently just us/ca/uk).

postalcodevalidate($ctry,$postcode)

Assumes the use of the international 2 letter country code e.g “us” for USA, “ca” for canada.

 //*** validates proper postal code formats for US/CA/UK 
 //***   $ctry is country code & $zip is postal code	
 //*
 //*** Use Example: 
 //*
 //* if (postcodevalidate($ctry,$zip){ 
 //*    //produce erreur 
 //*    } else { 
 //*            // do nothing is valid
 //*           }	
 //*
 //*
 //* Check my site http://www.reponetwork.com for latest 
 //* version. Feel free to place a link to my site on  
 //*     your site if you found this code useful.
 //*******************************************************
 
function postcodevalidate($ctry,$zip){

         $zip = trim($zip);
	 
	 switch($ctry){

                case "us":
	 
	          //** <12345> or <12345>-<1234> or <12345> <1234> **
		if (ereg("^[0-9]{5}$",$zip) || ereg("(^[0-9]{5})-([0-9]{4}$)",$zip) || ereg("(^[0-9]{5}) ([0-9]{4}$)",$zip)){ return false;} else { return true;}
	 
                case "ca":
	 
	          //** ** 
	        if (ereg("^[a-zA-Z][0-9][a-zA-Z][[:space:]][0-9][a-zA-Z][0-9]$",$zip)){ return false;} else { return true;}
	      
	        case "uk":
	      
	          //** <letter><letter><1 or 2 digitnumber> <number><letter><letter> **
	        if (ereg("^[a-zA-Z]{2}[0-9]{1,2} [0-9][a-zA-Z]{2}+$",$zip)){ return false;} else { return true;}
	    
	        default:
	        if (ereg("^[a-zA-Z0-9]+[a-zA-Z0-9- ]+[a-zA-Z0-9]$",$zip) && (strlen($zip) < 15)){ return false;} else { return true;}
	      
	        }       
	    
         }