input只能输入数字和一个小数点

参考

<el-input class="pro_input" v-model="formData.astrict" type="number" step="1" min="0" max="10000" size="mini" @keyup.native="proving1"></el-input>
methods :{
    proving1() {
        this.formData.astrict=this.formData.astrict.replace(/[^\.\d]/g,''); //清除"数字"和"."以外的字符
	this.formData.astrict=this.formData.astrict.replace(/^\./g,'');  //验证第一个字符是数字而不是.
	this.formData.astrict=this.formData.astrict.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的.
	this.formData.astrict=this.formData.astrict.replace(".","$#$").replace(/\./g,"").replace("$#$",".");//只允许输入一个小数点
	this.formData.astrict=this.formData.astrict.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
    }
}
<input oninput="value=value.replace(/[^0-9\.]/g,'').replace(/^\./g,'').replace(/\.{2,}/g,'.').replace('.','$#$').replace(/\./g,'').replace('$#$','.')" />
posted @ 2023-11-22 16:30  vvull  阅读(13)  评论(0编辑  收藏  举报