1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > vue element-ui实现input输入框金额数字添加千分位

vue element-ui实现input输入框金额数字添加千分位

时间:2022-04-19 07:55:06

相关推荐

vue element-ui实现input输入框金额数字添加千分位

在util.js中定义方法

包含金额添加过滤千分位,验证金额格式等

const MoneyTest = /((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/;// 金额添加千分位const comdify = function (n) {if(!n) return n;let str = n.split('.');let re = /\d{1,3}(?=(\d{3})+$)/g;let n1 = str[0].replace(re, "$&,");return str.length > 1 && str[1] ? `${n1}.${str[1]}` : `${n1}.00`;};//去除千分位中的‘,’const delcommafy = function (num){if(!num) return num;num = num.toString();num = num.replace(/,/gi, '');return num;};const valdateFn = function (rule,val,cb) {setTimeout(() => {if(val) {let inputVal = delcommafy(val);if (rule.test(inputVal)) {cb()} else {cb('只能是数字金额,最多两位小数')}}cb()})}// 验证金额数字可以为负数const moneyValid = function (rule,val,cb) {valdateFn(/((^-?[1-9]\d*)|^-?0)(\.\d{0,2}){0,1}$/,val,cb);};// 验证金额数字不可以为负数const moneyNValid = function (rule,val,cb) {valdateFn(MoneyTest,val,cb);};// 获取输入框的值const getInputValue = function (el) {let inputVal = el.target.value || '';return comdify(delcommafy(inputVal));};

在组件中使用

在template中

<el-input v-model.trim="form.pastAdjustFee" @blur="inputMoney($event,'pastAdjustFee')"></el-input>

在methods中定义

data(){return {form:{pastAdjustFee:''}}}methods:{inputMoney(el,name) {this.form[name] = getInputValue(el);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。