js限制输入框的字符数以及只允许输入数字实现

function limit(obj, limit) {
        var val = obj.value;
        if (len(val) > limit) {
            val=val.substring(0,limit);
            while (len(val) > limit){
                val = val.substring(0, val.length - 1);
            };
            obj.value = val;
        }
    }
    function clearNotNum(Obj){
        Obj.value = Obj.value.replace(/[^\d]/g,"");//清除“数字”以外的字符
    }

html

<p class="input clear "><span><input type="text" id="companyPhone" name="companyPhone" value="${nhProvider.companyPhone}" class="validate[required,custom[phone]]" onkeyup="limit(this, 16);clearNotNum(this);" onpaste="limit(this, 16);clearNotNum(this);"S/></span></p>

猜你喜欢

转载自www.cnblogs.com/pypua/p/12897197.html
今日推荐