Magento-1.8 – Fixing Zip/Postal Code Validation Issues

magento-1.8php-5.4validation

In billing.phtml file Zip/Postal Code validation is not working properly.

1) When I field is empty it's asking it's required fine.

2) But letter's also allowing this field. I don't want to allow letters only Numeric values will allow.

my code is

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

and

I set to true in javascript also

var billingForm = new VarienForm('co-billing-form',true);

May I know Where I went wrong? or how to do this?

Thanks.

Best Answer

You should be able to change the class from "validate-zip-international" to "validate-number" to add Magento's frontend JS validation for numbers, or "validate-digits".

Source: /js/prototype/validation.js for a list of all validation classes (addAllThese method)

['validate-number', 'Please enter a valid number in this field.', function(v) {
    return Validation.get('IsEmpty').test(v) || (!isNaN(parseNumber(v)) && !/^\s+$/.test(parseNumber(v)));
}],
['validate-digits', 'Please use numbers only in this field. Please avoid spaces or other characters such as dots or commas.', function(v) {
     return Validation.get('IsEmpty').test(v) ||  !/[^\d]/.test(v);
}]