首页 > 其他 > 详细

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

时间:2019-12-21 15:31:22      阅读:1114      评论:0      收藏:0      [点我收藏+]

仅个人经验,希望能帮到有需要的人。

第一次写 就话不多说了直接上代码。

<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)
                        }
                    }
                }
            },

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

原文:https://www.cnblogs.com/zm-97/p/12076892.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!