只要有第三位就清除:
<input type="text" onkeyup="value=value.replace(/\.\d{2,}$/,value.substr(value.indexOf('.'),3))" />
jquery validate 验证:
jQuery.validator.addMethod("lrunlv",function(value,element) { return this.optional(element) || /^\d+(\.\d{1,2})?$/.test(value); },"小数位不能超过三位");
//验证小数点后的位数 jQuery.validator.addMethod("decimals",element,d){ var a = value.indexOf(".")+1; if(a==0){ a=value.length; } var b = value.length; var c = b-a; return this.optional(element) || c<=d; });原文链接:https://www.f2er.com/regex/363243.html