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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

海里的鱼-L   2019-12-21 我要评论

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

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

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们