Jump to content

How to add additional validation on address fields in checkout


pdaulerio

Recommended Posts

Hi all, I’m wondering if anyone can help this poor newb…our site is Australian based and I’m wanting to validate the postal / zip code on the checkout page (address.php) so that if someone selects Australia as the country then the system will validate the following for the postal / zip code field:
1. The value entered must be numeric;
2. The maximum number of characters is 4; and
3. Trim (truncate) any spaces.

Currently if someone enters an invalid postcode the shipping module does not calculate correctly. Can anyone help?

I’ve tried to put validation in the relevant php files but without success, as well as trying to do it via javascript in the address.tpl but I don’t know how to adapt the javascript to enter it into the .tpl file??

Link to comment
Share on other sites

I did not find any hooks in address.php so I do not know how to make module for the address fields validation (may be experts know?)

I suggest to add javascript to themes/your_theme/address.tpl
This script has to add event listeners to "postcode" and "id_country" fields

After changing country to Austalia script should enable validation for "postcode"

Link to comment
Share on other sites

add following script to address.tpl at the top of file after the row var baseDir = '{$base_dir_ssl}';:

{literal}
   var sm_tools={
       checkInteger: function(evt){
           evt = (evt) ? evt : window.event;
           var charCode = (evt.which) ? evt.which : evt.keyCode;
           if (charCode==35 || charCode==36 || charCode==37 || charCode==39) return true; //cursor movement
           else{
               if (charCode > 31 && (charCode < 48 || charCode > 57)) {
                   return false;
               }
           }
           return true
       }
   }

   $(document).ready(function(){
       $('#postcode').keypress(function(event){return sm_tools.checkInteger(event)})
   })
{/literal}



This script allows only digits to be typed into postcode field.

May be later I'll make the similar function for my site with more complicated behaviour (with different countries rules)

Link to comment
Share on other sites

worked a treat. BIG thankyou. If you do happen to work out how to include additional logic based on country selection, please post back. I'm wanting to restrict to numeric values only for Australia and a max length of 4 chars, but allow alphanumeric for other countries and cap the length to 10 chars.

Link to comment
Share on other sites

    var sm_tools={
       checkInteger: function(evt){

           var selCountry = document.getElementById("id_country");
           id_country = selCountry.options[selCountry.selectedIndex].value;

           var allowedChars, maxLength;
           if (id_country == 24){ //Australia
               allowedChars = '0123456789';
               maxLength = 4;
           }
           else if (id_country = 177){ //Russia
               allowedChars = '0123456789';
               maxLength = 6;
           }
           else{
               allowedChars = '0123456789abcdefghjklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
               maxLength = 10;
           }

           evt = (evt) ? evt : window.event;
           var charCode = (evt.which) ? evt.which : evt.keyCode;

           cursorCodes=[35,36,37,39,8,46]; //cursor movement
           if ($.inArray(charCode,cursorCodes)>-1) return true;
           else{
               if (evt.target.value.length > maxLength-1)
                   return false;

               if (allowedChars.indexOf(String.fromCharCode(charCode)) == -1){
                   return false;
               }
           }
           return true;
       }
   }

   $(document).ready(function(){
       $('#postcode').keypress(function(event){return sm_tools.checkInteger(event)})
   })



id_country is from default installation. Change it in script if you have modified country list.
In my theme I moved country and state fields before the first address field in order to have country selected before post code.

If you want to check validity of post code it is necessary to use change event of postcode field or to do check before form sending. And it is possible to load city based on post code but it's another story

Link to comment
Share on other sites

  • 6 years later...

Hi All,

I have a requirement to validate the Pincode from the address saved. If the product is not available it should not allow to payment page after clicking proceed to check out button rather it should show an error message that the product is not available on the given pincode. Any help would be appreciated. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...