View Single Post
  #6  
Old 06-07-2004, 01:03 PM
 
NuAlpha NuAlpha is offline
 

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

Default

Please note that the module I posted here earlier is non-functional and will not properly check zip codes. I left out a pair of parenthesis in the preg_match() statement.

Here is the fully functional, Canadian postal code compatible, version...

In register.php, look for the line "$smarty->assign("error",$error);". Put the following updated function call directly under that:
Code:
# Check the zip code for errors. if (@include('validate_zipcode.php')) { $b_zipcode = validate_zipcode($b_zipcode,$b_country); if (!empty($s_zipcode)) $s_zipcode = validate_zipcode($s_zipcode,$s_country); else $s_zipcode = $b_zipcode; }

If you have not already done so, create a file named "validate_zipcode.php" in the "include" directory of Xcart. Fill that file with the following code:
Code:
<?php ######################################### ## ## Validate Zipcode ## ## ######################################### ## Version: 1.2.4 (3/2/2004) ## Last updated: 6/7/2004 if ( !defined('XCART_SESSION_START') ) { header("Location: ../"); die("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? # Prevent char deletion by concatenation during next part of validation code. Instead, cause an error. if (preg_match("/[-._\/\ ]/",$checkZip[2])) { 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. $zipcode = ''; } else $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 (preg_match("/[-._\/\ ]/",$checkZip[2])) { if (empty($checkZip[2]) || $checkZip[2] != ' ') $checkZip[2] = ' '; // Ensure that a space is between the two code segments. $zipcode = strtoupper($checkZip[1].$checkZip[2].$checkZip[3]); } else $zipcode = ''; // Cause an error. } else $zipcode = ''; } else $zipcode = ''; break; } return $zipcode; } ?>

There have been no changes to "check_zipcode_js.tpl" since the original posting of the code.

Let me know if you see any further problems.

I setup this version to switch via country so that you can easily add another "case" to the conditional statement for postal codes from countries not included in this release. If you do add postal code checks for more countries, please do us all a favor and post them here.

As stated before, I do give the Xcart developers explicit permission to use this code in any future release of Xcart.
__________________
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