![](/img/trans.png)
[英]restrict the “textfield” to accept only digits and maximum 10 digits in magento form
[英]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.