News:

Looking for documentation? Take a look on our wiki

Main Menu

zip code validation for shipping by zip code

Started by simbo1905, May 13, 2006, 16:42:18 PM

Previous topic - Next topic

simbo1905

we have  by virtuemart calcuate shipping cost by weight and zip code. as we are only ship to the US (we deleted all the other countries from jos_vm_country) our shipping cost tables uses the first five digits of the zip code. customers could however prefix their zip with the state initials and add an extra four digits to the end (which dont effect shipping at all). to get around this we have the code force the user to use a certain zip code format. to do this in htmlTools.class.php just below:
      if( in_array( 'email', $required_fields)) {
      
         echo '
         if( !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(form.email.value))) {
            alert( \''. html_entity_decode( _REGWARN_MAIL ).'\');
            return false;
         }';

      }

we have added
      if( in_array( 'zip', $required_fields)) {
         echo '
         if( !('.PSHOP_ZIP_REGEX.'.test(form.zip.value))) {
            alert( \''. html_entity_decode( _REGWARN_ZIP ).'\');
            return false;
         }';
      }

where we have defined _REGWARN_ZIP in english.php to be some sensible warning. we have also defined PSHOP_ZIP_REGEX in virtuemart.cfg.php to be:
define('PSHOP_ZIP_REGEX', '/[0-9][0-9][0-9][0-9][0-9]/'); // make ship price by weight and zip code easier by forcing five digits

but if we were in the UK and just wanted to check that it is a valid zip code we could have used a regular expression found at http://regexlib.com such as:
define('PSHOP_ZIP_REGEX', '/^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$/');

search for zip at regexlib.com to find full US zip code checking regular expressions. then define them with a statement above adding a '/' character to either end.

bob803

Thank you, this is a good fix for us, we had people using their state code in the zip and we use flat rate shipping, so when they did that they got free shipping, I was not happy.


enolbos

How can I do multiple countries?  I.e. USA and Canada.

Thanks,
Brian

Jumpman

The validation part of this works fine for me, but the alert that appears simply says, "_REGWARN_ZIP."  I've looked over the code and I can't figure it out. ideas?

DRACULINOS

#4
You are getting this error because the right code is

if( in_array( 'zip', $required_fields)) {
          echo '
          if( !('.PSHOP_ZIP_REGEX.'.test(form.zip.value))) {
             alert( \''. html_entity_decode( $VM_LANG->_REGWARN_ZIP ).'\');
             return false;
                      }';
         }

Maybe its late for you but I hope it helps some others facing the same problem.  ;)

vmzoid

this one worked for me

in administrator/components/com_virtuemart/classes/ps_userfield.php add:

// ZIP validation. Regex from http://regexlib.com/REDetails.aspx?regexp_id=459
        if( isset( $required_fields['zip'] )) {

echo '
if( !(/^[0-9]{5}([\s-]{1}[0-9]{4})?$/.test(form.zip.value))) {
alert( \'Please enter a valid US 5 Digit Zip Code or US 5 Digit Zip Code + 4\');
return false;
}';

}


around line 720
whoop-dee koo!!

miguelgarcia

#6
Hello to all and, obviously, thanks to all...

Just adding some more help!!!

I think user simbo1905 (who started this post) forgot to tell us two things:


* One of them was corrected by DRACULINOS full member, who added $VM_LANG-> before the _REGWARN_ZIP , resulting in:


alert( \''. html_entity_decode( $VM_LANG->_REGWARN_ZIP ).'\');


* The other one was when he told us "we have defined _REGWARN_ZIP in english.php", so I started to think: But which one, there are several english.php files in language directory.

After put my Windows Vista to look also inside .php files and do a search for REGWARN_MAIL (which already exists by default in virtuemart), I found the english.php file wich contains REGWARN_MAIL (so I can define REGWARN_ZIP just bellow REGWARN_MAIL) is in

\administrator\components\com_virtuemart\languages\common

This can be obvious for many people but not for many other ones.


Miguel Garcia


miguelgarcia

Hello to all and, specially to DRACULINOS who I'm waiting for ...

Introduction
------------

I needed to validate postal code to one of my clients who is Selling only in Portugal, so I used vmzoid member soluction with success, after adapting the validation regular expression to Portugal postal code.


Problem
-------

As other clients of mine are selling to other countries I needed to think about a more complex solution, which permits the warning messages in different languages, like the one presented by member simbo1905, who started this post.

My questions are all about the implementation of this solution in Virtuemart 1.1.4 and about all the problems that came across during trying out of this.


I will try to be the most objective I can, so I will divide all my questions about what simbo1905 member suggested, in numbered ones:


Note I'm taking about VirtueMart 1.1.4


1- simbo1905 said : " to get around this we have the code force the user to use a certain zip code format. to do this in htmlTools.class.php just below: "

I tried to find anything like

     if( in_array( 'email', $required_fields)) {
   
        echo '
        if( !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(form.email.value))) {
           alert( \''. html_entity_decode( _REGWARN_MAIL ).'\');
           return false;
        }';

     }


in htmlTools.class.php file, but found nothing. Anyway I found something identical in ps_userfield.php as follows (with little differences)


if( isset( $required_fields['email'] )) {

              echo '
           if( !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(form.email.value))) {
               alert( \''. str_replace("'","\\'",$VM_LANG->_('REGWARN_MAIL',false)) .'\');
               return false;
           }';

       }


Question 1: Can I consider in VirtueMart 1.1.4 ps_userfield.php file as being the right one to add the simbo1905 member's sugested code (as seen bellow) just after the email validation?

if( in_array( 'zip', $required_fields)) {
        echo '
        if( !('.PSHOP_ZIP_REGEX.'.test(form.zip.value))) {
           alert( \''. html_entity_decode( _REGWARN_ZIP ).'\');
           return false;
        }';
     }


2- simbo1905 said :  "   we have also defined PSHOP_ZIP_REGEX in virtuemart.cfg.php to be

define('PSHOP_ZIP_REGEX', '/[0-9][0-9][0-9][0-9][0-9]/'); //        or  


define('PSHOP_ZIP_REGEX', '/^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$/');    "



Question 2 : In virtuemart.cfg.php  I tryed to find any REGWARN or REGWARN_MAIL  words, so I can define PSHOP_ZIP_REGEX after REGWARN_MAIL definition, but found nothing.

In VirtueMart 1.1.4 , is the file  virtuemart.cfg.php  the one where I need to define  PSHOP_ZIP_REGEX or in this recent version of VirtueMart I need to do it in another file ?

And ...  As I have no example of REGWARN_MAIL defined at virtuemart.cfg.php , so I can have a reference on to define PSHOP_ZIP_REGEX, where, inside the right file, must I add the  PSHOP_ZIP_REGEX definition?



3- Question about another doubt of mine, not about simbo1905's member post:

When Selling to multiple countries, being shipping costs dependent on weight and also on postal code, how to put VirtueMart make the calculations on the multiple postal code formats and comparing them to defined shipping rates (as ones will have only numeric characters, other will have alphanumeric characters, others will have separation space or "-" and even others will have no spaces) ?


Lots of thanks to all, in advance again ...


Miguel Garcia