vue+element 中 el-input框 限制只能输入数字及一位小数

仅我的经验,但愿能帮到有须要的人。html

第一次写 就话很少说了直接上代码。this

<el-input @keyup.native="proving(index)" v-model="item.Price"></el-input>
// 只能输入数字且只有一位小数
 proving(e) {           // this.form.skus[e].Price 是input的值  
                // 先把非数字的都替换掉,除了数字和.
                this.form.skus[e].Price = this.form.skus[e].Price.replace(/[^\d.]/g, ''); // 必须保证第一个为数字而不是.
                this.form.skus[e].Price = this.form.skus[e]Price.replace(/^\./g, ''); // 保证只有出现一个.而没有多个.
                this.form.skus[e].Price = this.form.skus[e].Price.replace(/\.{2,}/g, ''); // 保证.只出现一次,而不能出现两次以上
                this.form.skus[e].Price = this.form.skus[e].Price.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.'); let index = -1
                for (let i in this.form.skus[e].Price) { if (this.form.skus[e].Price[i] === '.') { index = i } if (index !== -1) { if (i - index > 1) { this.form.skus[e].Price = this.form.skus[e].Price.substring(0, this.form.skus[e].Price.length - 1) } } } },

原文出处:https://www.cnblogs.com/zm-97/p/12076892.htmlspa

相关文章
相关标签/搜索