Call-back icon  Sales: Call 877.832.5289 (N America)|310.295.4144 (International)

Magento

eCommerce Software for Online Growth

Magento Forum

   
Zip code only accept in this format 99999-999 … HOW? 
 
tubadc
Member
 
Total Posts:  51
Joined:  2008-05-07
 

How can i add a filter to all zip codes fields for only accept numbers and in this format 99999-999, tks…

 
Magento Community Magento Community
Magento Community
Magento Community
 
lisali
Guru
 
Avatar
Total Posts:  459
Joined:  2008-04-28
London, UK
 

Hi,

Go to:

app/design/frontend/default/default/template/checkout/cart/shipping.phtml

You will need to edit the javascript validation that is at the very bottom of the page.

Good luck!
Lisa

 Signature 

ExtraCall.com - UK Telecommunications & VOIP

 
Magento Community Magento Community
Magento Community
Magento Community
 
Rogue
Jr. Member
 
Total Posts:  1
Joined:  2008-06-18
Loveland, OHIO, USA
 

Why would you want to do this?  Most people (I think) do not know their full zip code.  Why make it harder to order something when it is not necessary?

Richard

 Signature 

Richard
http://planetgps.net
http://roguewavemarine.com

 
Magento Community Magento Community
Magento Community
Magento Community
 
Frikki
Jr. Member
 
Avatar
Total Posts:  20
Joined:  2007-09-25
Reykjavík, Iceland
 

This is rather ridiculous, as the zip code validation should change according to country selection, but instead is a hard-coded regexp that only seems to accommodate US zip codes. Say, you have a store that ships to 4 different countries, i.e. Iceland, Denmark, Sweden and Norway, then you need to have checks for Iceland, which is 3 digits and never starting with a zero. Denmark would be a 4 digit check, also not beginning with a zero. Sweden have 5 digits in their postal codes, but have a space following the first 3 digits, like this for example 100 12.

It seems rather daunting task to try to accomplish a fix, wherefore the solution would be to simply bypass the JavaScript validation. If you despite the tedious work feel like making the checks, the script provides a mechanism for constructing the different checks in an array. In each array item you could include a check for the value selected in the country field. Note that the country_id uses short country codes, like DK for Denmark etc.

I put together a simple example that illustrates the concept:

<script type="text/javascript">
    var 
coShippingMethodForm = new VarienForm('shipping-zip-form');
    
Validation.addAllThese(
    
[
           [
'validate-postcode''<?php echo $this->__('Please enter a valid zip code. For example 90602 or 90602-1234.') ?>', function(v{
                
var country_id 'IS'
                
var country = $('country');
                var 
element = $('postcode');
                if (
element && ('' != element.value) && (country_id == country.value)) {
                    
if (!element.value.match(/(^[1-9]{1}[0-9]{2}$)/ )) {
                        
return false;
                    
}
                }
                
return true;
           
}],
           
['validate-postcode''<?php echo $this->__('Please enter a valid zip code. For example 90602 or 90602-1234.') ?>', function(v{
                
var country_id 'DK'
                
var country = $('country');
                var 
element = $('postcode');
                if (
element && ('' != element.value) && (country_id == country.value)) {
                    
if (!element.value.match(/(^[1-9]{1}[0-9]{3}$)/ )) {
                        
return false;
                    
}
                }
                
return true;
           
}]
    ]
    
);
</script>

The first array item checks for Icelandic postal codes (3 digits) and the second checks for Danish postal codes (4 digits). Note that you would also have to construct separate warning messages.

The script could be optimized for checking regexp on multiple countries at once that uses same pattern postal codes, but that might come later.

The file you need to edit is template/checkout/cart in your frontend interface at around line 92 (Modern Theme v.1.1.6).

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
 
Sales: Call 877.832.5289 (North America) 310.295.4144 (International)
© Copyright 2008 Varien. Magento, eCommerce software, is a trademark of Irubin Consulting Inc. DBA Varien
Privacy Policy|Terms of Service
Magento Community Count
52899 users|606 users currently online|106706 forum posts