View Single Post
  #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