Reputation: 911
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
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
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
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