<a-input @input="FC.numDxsCheck(form, 2, 'name')" v-model="form.name" :maxLength="50" placeholder="请输入" />
/**
* 带小数点的数字输入 - 默认4为小数
* 例如:FC.numDxsCheck(form, 3,'phone')
* form:表单对象,其中包括phone
* ws:几位小数
* key:键名不叫phone时需要传入新的键名
* */
export function numDxsCheck(form, ws, key){
if(key === undefined){
key = "cost";
}
form[key] = form[key] + "";
//先把非数字的都替换掉,除了数字和.
form[key] = form[key].replace(/[^\d.]/g,"");
//必须保证第一个为数字而不是.
form[key] = form[key].replace(/^\./g,"");
//保证只有出现一个.而没有多个.
form[key] = form[key].replace(/\.{2,}/g,".");
//保证.只出现一次,而不能出现两次以上
form[key] = form[key].replace(".","$#$").replace(/\./g,"").replace("$#$",".");
//只能输入4个小数
let c = null;
switch(ws) {
case 1:
c = /^(\-)*(\d+)\.(\d).*$/;
case 2:
c = /^(\-)*(\d+)\.(\d\d).*$/;
break;
case 3:
c = /^(\-)*(\d+)\.(\d\d\d).*$/;
break;
case 4:
c = /^(\-)*(\d+)\.(\d\d\d\d).*$/;
break;
default:
c = /^(\-)*(\d+)\.(\d\d).*$/;
}
//只能输入两个小数
form[key] = form[key].replace(c,'$1$2.$3');
}
网友评论