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,"");//清除“数字”以外的字符 }
function clearNoNum(obj){
obj.value = obj.value.replace(/[^d.]/g,""); //清除“数字”和“.”以外的字符
obj.value = obj.value.replace(/.{2,}/g,"."); //只保留第一个. 清除多余的
obj.value = obj.value.replace(".","$#$").replace(/./g,"").replace("$#$",".");
obj.value = obj.value.replace(/^(-)*(d+).(dd).*$/,'$1$2.$3');//只能输入两个小数
if(obj.value.indexOf(".")< 0 && obj.value !=""){//以上已经过滤,此处控制的是如果没有小数点,首位不能为类似于 01、02的金额
obj.value= parseFloat(obj.value);
}
}
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>
或者
<p class="input clear "><span><input type="text" id="companyPhone" name="companyPhone" value="${nhProvider.companyPhone}" class="validate[required,maxSize[13]]" onkeyup="(this.v=function(){this.value=this.value.replace(/[^0-9|-]+/,'');}).call(this)" onblur="this.v();"/></span></p>