cancel
Showing results for 
Search instead for 
Did you mean: 

ZIP Code Validation

SOLVED

ZIP Code Validation

I just noticed that in our order check out process that a user can enter a 100+ character zip code(after looking at the design of the MYSQL table I think they can enter up to 255 characters) and the system will accept that zip code.  I would expect that their would be a validation process designed into the software but I guess not.  How can I add a validation for a zip code?

I would appreciate any comments please...

1 ACCEPTED SOLUTION

Accepted Solutions

Re: ZIP Code Validation

Hello,

 

Seem that you use Community version. You can take a look the validation js file: <Magento_Root>/js/prototype/validation.js.  As we can see, there are many exist validation classes, we can use them or add a new custom validation class. Magento validation bases on the html classes, so we need to add a custom class to our html inputs and add a custom validation script to global Magento validation object.

For example, I use rwd package and enable Guest Checkout. I'm going to add the class for validating the length of zip code.

 

#app/design/frontend/rwd/default/template/persistent/checkout/onepage/billing.phtml

<script type="text/javascript">
Validation.addAllThese([
    ['validate-zip-code-range', 'Text length should be less than 10 characters.', function (v) {
        var result = true;
        if(v.length > 10) {
            //Validate fail
            result = false;
        }
        return result;
    }]
])
</script>

And, find zip code input to add validate-zip-code-range

#app/design/frontend/rwd/default/template/persistent/checkout/onepage/billing.phtml

<div class="input-box"> <input type="text" title="<?php echo $this->quoteEscape($this->__('Zip/Postal Code')) ?>" name="billing[postcode]" id="billing:postcode" value="<?php echo $this->escapeHtml($this->getAddress()->getPostcode()) ?>" class="input-text validate-zip-code-range validate-zip-international <?php echo $this->helper('customer/address')->getAttributeValidationClass('postcode') ?>" /> </div>


The result:

 

Problem solved? Click Accept as Solution!

View solution in original post

5 REPLIES 5

Re: ZIP Code Validation

You can set a vaidation, following this process : open Customers > Attributes > Customer adress attributes
Then select your zip code field, and on the field configuration, you can define input validation, min and max length, as well as a validation that you can define separately as a RegEx if you need more detailled validation rule.

French Magento Solution Specialist.
Magento consultancy

Re: ZIP Code Validation

Hello,

 

Seem that you use Community version. You can take a look the validation js file: <Magento_Root>/js/prototype/validation.js.  As we can see, there are many exist validation classes, we can use them or add a new custom validation class. Magento validation bases on the html classes, so we need to add a custom class to our html inputs and add a custom validation script to global Magento validation object.

For example, I use rwd package and enable Guest Checkout. I'm going to add the class for validating the length of zip code.

 

#app/design/frontend/rwd/default/template/persistent/checkout/onepage/billing.phtml

<script type="text/javascript">
Validation.addAllThese([
    ['validate-zip-code-range', 'Text length should be less than 10 characters.', function (v) {
        var result = true;
        if(v.length > 10) {
            //Validate fail
            result = false;
        }
        return result;
    }]
])
</script>

And, find zip code input to add validate-zip-code-range

#app/design/frontend/rwd/default/template/persistent/checkout/onepage/billing.phtml

<div class="input-box"> <input type="text" title="<?php echo $this->quoteEscape($this->__('Zip/Postal Code')) ?>" name="billing[postcode]" id="billing:postcode" value="<?php echo $this->escapeHtml($this->getAddress()->getPostcode()) ?>" class="input-text validate-zip-code-range validate-zip-international <?php echo $this->helper('customer/address')->getAttributeValidationClass('postcode') ?>" /> </div>


The result:

 

Problem solved? Click Accept as Solution!

Re: ZIP Code Validation

Unable o find Customer > Attributes...I t might be because of the template or version of Magento?

Community edition, Ultimo 1.11.2 Template.

I found Configuration, Customer..but nothing related to attributers.

 

Thank you for the suggestion.

Re: ZIP Code Validation

I am reviewing your suggestion and I have found what looks to be the same file, it does have some validation scripts added but nothing for zip codes, it pretty much only has "required filed" only.  I think this may be the solution.  I'll do some testing.

 

Thank you

Re: ZIP Code Validation

Thanks! Works for me