繁体   English   中英

验证文本字段以限制6位数字

[英]Validation for textfield to restrict for 6 digits

我们需要在magento网站的结帐过程中在textfield:“邮政编码”中设置验证。

我需要将此文本字段限制为最多6位数字。

我为此使用以下代码:

    <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"/>

请帮助我验证“ 仅限6位数字”

尝试这个

 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"/>

这是验证10位数电话号码的示例:

HTML:

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

脚本文件:

<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>

更好的方法是使用pattern属性

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM