View Single Post
  #35  
Old 02-07-2005, 04:05 PM
 
NuAlpha NuAlpha is offline
 

X-Adept
  
Join Date: Aug 2003
Location: US
Posts: 598
 

Default

Here is a minor update to take care of a problem with Canadian zip code validation...
Code:
<?php ######################################### ## ## Validate Zipcode ## ## ######################################### ## Version: 1.2.8 (3/2/2004) ## Last updated: 2/7/2005 if (!defined('XCART_SESSION_START')) { header('Location: ../'); exit('Access Denied!'); } function validate_zipcode($zipcode,$country='') { $zipcode = trim($zipcode); // Prep by removing excess spaces. switch ($country) { case 'US': // Validate United States postal codes. if (strlen($zipcode) > 5 && strlen($zipcode) <= 10) { // If not length 5, then check for valid format. if (preg_match('/^([0-9]{5})([-=._\/\ ])?([0-9]{4})?$/', $zipcode, $checkZip)) { if (!empty($checkZip[3])) { // Is it a zip+4 code? if (empty($checkZip[2]) || $checkZip[2] != '-') $checkZip[2] = '-'; // Ensure that a dash is present in 9 digit zip code. $zipcode = $checkZip[1].$checkZip[2].$checkZip[3]; } else // Cause an error from incorrect length. $zipcode = ''; } else // No valid zip code matched. $zipcode = ''; } elseif (strlen($zipcode) < 5 || preg_match('/[[:alpha:]]/i',$zipcode)) // Check min length or presence of a letter. $zipcode = ''; # If none of the above, do not modify zipcode. It is a valid 5 numeric digits. break; case 'CA': // Validate Canadian postal codes. if (strlen($zipcode) >= 6 && strlen($zipcode) <= 7) { // Check for valid length. if (preg_match('/^([A-z0-9]{3})([-._\/\ ]?)([A-z0-9]{3})$/i', $zipcode, $checkZip)) { if ($checkZip[2] != ' ') $checkZip[2] = ' '; // Ensure that a space is between the two code segments. $zipcode = $checkZip[1].$checkZip[2].$checkZip[3]; } else // Cause an error from incorrect length. $zipcode = ''; } else // No valid zip code matched. $zipcode = ''; if (!empty($zipcode)) $zipcode = strtoupper($zipcode); // Capitalize alphabetic characters for Canadian postal code. break; } return $zipcode; } ?>
__________________
X-Cart Pro 4.5.5 Platinum
X-Payments 1.0.6
PHP 5.3.14
MySQL 5.1.68
Apache 2.2.23
Reply With Quote