输入框 - 限制输入两位小数

做两位小数校验
简单记录一下

 input(val){
     this.value = this.value+''
     this.value = this.value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
     this.value = this.value.replace(/^\./g,""); //验证第一个字符是数字而不是.
     this.value = this.value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的.
     this.value = this.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");//只允许输入一个小数点
     this.value = this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
},
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容