Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls

ZipCode Validation Module

 
Reply
   X-Cart forums > X-Cart 4 > Dev Questions
 
Thread Tools Search this Thread
  #1  
Old 06-03-2004, 08:53 AM
 
NuAlpha NuAlpha is offline
 

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

Default ZipCode Validation Module

I really appreciate all of the help I have received here on the forums during the development of our site. I know this is a small gesture, but I wanted to show that appreciation by posting our Zipcode Validation module.

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

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.1.2 (3/2/2004) ## Last updated: 6/4/2004 if ( !defined('XCART_SESSION_START') ) { header("Location: ../"); die("Access Denied!"); } function validate_zipcode($zipcode) { if (strlen($zipcode) != 5) { // If not length 5, then check for valid format. preg_match("/^([0-9]{5})[-._\/\]?([0-9]{4})?/", $zipcode, $checkZip); if ($checkZip[2]) { // Is it a zip+4 code? $checkZip[2] = '-'; // Ensure that a dash is present in 9 digit zip code. $zipcode = $checkZip[1].$checkZip[2].$checkZip[3]; // Fix potential dash problem. if (!$checkZip[3]) { $zipcode = ''; // Problem with last 4 digits, so cause an error. } } elseif (!$checkZip[1]) { // Is there an error with the 5 digit zip code? $zipcode = ''; // Cause an error. } } if (preg_match("/[[:alpha:]]+/i",$zipcode)) { $zipcode = ''; // There is a letter in the zip code, so cause an error. } return $zipcode; } ?>

Here is check_zipcode_js.tpl. I am unsure if the javascript is still having some problems, but I have not run into any after the latest revisions.
Code:
<script> {literal} function check_zip_code_field(cnt, zip) { if (cnt.options[cnt.selectedIndex].value=="US") { // Check if the zip code is 5 or 9 digits. if (zip.value.length != 5 && zip.value != "") { if (zip.value.length != 9 && zip.value.length != 10 && zip.value != "") { alert("Ensure that you have 5 or 9 digits in your zip code.\nFor Example: '12345' or '12345-6789'") zip.focus() return false } } } else if (cnt.value == "CA") { if (zip.value.length != 6 && zip.value.length != 7 && zip.value != "") { alert("Ensure that you have 6 or 7 characters in your postal code.") zip.focus() return false } } return true } function check_zip_code() { return check_zip_code_field(document.forms["registerform"].b_country, document.forms["registerform"].b_zipcode) && check_zip_code_field(document.forms["registerform"].s_country, document.forms["registerform"].s_zipcode); } {/literal} </script>

And that is it. Enjoy!

Ohh, and by the way...I do give the Xcart development team explicit permission to include this into their Xcart source code, and alter it to their hearts content.
__________________
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
  #2  
Old 06-06-2004, 09:23 AM
 
GM GM is offline
 

eXpert
  
Join Date: Mar 2004
Location: Canada
Posts: 293
 

Default

Thanks for the post, do you have any Idea how I would include Canadian postal codes as well?
__________________
v. 4.0.14 (GM Style)
O.S. Linux
Build Your Own Diamond Ring
Reply With Quote
  #3  
Old 06-06-2004, 10:27 AM
  kpayne's Avatar 
kpayne kpayne is offline
 

X-Adept
  
Join Date: Dec 2002
Location: Firetanksoftware.com
Posts: 469
 

Default

It's a bigger mod, but I've also done something like this with the data from www.zipcodedownload.com. You have to pay for the database, but with that info I'm able to verify their city / state match their zipcode. It saves a ton of money on reshipping packages and changing addresses en route.
__________________
X-cart Featured Products Manager from http://www.firetanksoftware.com - Put your products where you want, how you want.
Reply With Quote
  #4  
Old 06-06-2004, 10:30 AM
  kpayne's Avatar 
kpayne kpayne is offline
 

X-Adept
  
Join Date: Dec 2002
Location: Firetanksoftware.com
Posts: 469
 

Default

I just checked out their site again and they now have info for Canada as well. They didn't have this when I bought the U.S. info.
__________________
X-cart Featured Products Manager from http://www.firetanksoftware.com - Put your products where you want, how you want.
Reply With Quote
  #5  
Old 06-06-2004, 11:26 AM
 
NuAlpha NuAlpha is offline
 

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

Default

Quote:
Originally Posted by GM
Thanks for the post, do you have any Idea how I would include Canadian postal codes as well?

I'd have to rewrite it a bit to get Canadian codes to work for this. I am going to have to do that because we do sell to Canada.

I will post it once I get it finished.
__________________
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
  #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
  #7  
Old 06-08-2004, 10:55 AM
 
NuAlpha NuAlpha is offline
 

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

Default

Another update for this module. Better detection of one too many characters in postal codes and additional correction of 'fat-fingering' the dash.

Code:
<?php ######################################### ## ## Validate Zipcode ## ## ######################################### ## Version: 1.2.6 (3/2/2004) ## Last updated: 6/8/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 or overflow during next part of validation code. Instead, cause an error. if (empty($checkZip[4])) { // This checks if the last digit will be cutoff from an empty $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 because there are 10 digits, instead of 9 with a partitioning character. $zipcode = ''; } 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 (empty($checkZip[4])) { 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 because there are 7 digits, instead of 6 with a partitioning character. $zipcode = ''; } 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; } ?>

Again, let me know if you have problems with this so that I can correct any bugs.
__________________
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
  #8  
Old 06-08-2004, 12:53 PM
 
GM GM is offline
 

eXpert
  
Join Date: Mar 2004
Location: Canada
Posts: 293
 

Default

WOOOO HOOOOO!!! Thanks! Very, Very AWESOME!

I am still not sure how "check_zipcode_js.tpl" is called? Where do I put it? Can someone please explain it to me.
__________________
v. 4.0.14 (GM Style)
O.S. Linux
Build Your Own Diamond Ring
Reply With Quote
  #9  
Old 06-12-2004, 09:22 AM
 
NuAlpha NuAlpha is offline
 

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

Default

Quote:
Originally Posted by GM
I am still not sure how "check_zipcode_js.tpl" is called? Where do I put it? Can someone please explain it to me.

You put it in the root of your skins directory. There should already be one there, so just replace the code with what I posted.
__________________
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
  #10  
Old 06-12-2004, 01:48 PM
 
GM GM is offline
 

eXpert
  
Join Date: Mar 2004
Location: Canada
Posts: 293
 

Default

OK... THAT DOES IT! I'M OFFICIALLY A NEWBIE AGAIN!
How embarrassing man... DOH!
__________________
v. 4.0.14 (GM Style)
O.S. Linux
Build Your Own Diamond Ring
Reply With Quote
Reply
   X-Cart forums > X-Cart 4 > Dev Questions


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 10:23 PM.

   

 
X-Cart forums © 2001-2020