fresher
fresher

Reputation: 911

Validation for textfield to restrict for 6 digits

we need to set validation in textfield : "zip code" under checkout process in magento site.

i need to restrict this textfield for maximum 6 digits.

i am using following code for this :

    <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') ?>" placeholder="Postal code"/>

please help me to give validation for Restricting only for 6 digits

Upvotes: 1

Views: 3993

Answers (3)

Tuan Ha
Tuan Ha

Reputation: 630

The better way is use the pattern attribute:

<input type="number" maxlength="6" pattern="[0-9]{6,}" placeholder="Zip code" required>

Upvotes: 2

Rahul Gopi
Rahul Gopi

Reputation: 412

This is an example for validating phone number of 10 digits:

Html :

<input type="text" id="phone" name="phone" onkeypress="phoneno()" maxlength="10">

Script file:

<script>        
       function phoneno(){          
        $('#phone').keypress(function(e) {
            var a = [];
            var k = e.which;

            for (i = 48; i < 58; i++)
                a.push(i);

            if (!(a.indexOf(k)>=0))
                e.preventDefault();
        });
    }
   </script>

Upvotes: 1

Denis Radinski
Denis Radinski

Reputation: 100

try this

 function limit(element)
    {
        var max_chars = 6;

        if(element.value.length > max_chars) {
            element.value = element.value.substr(0, max_chars);
        }
    }

 <input type="text" onkeydown="limit(this);" onkeyup="limit(this); "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') ?>" placeholder="Postal code"/>

Upvotes: 2

Related Questions