vue input框数字后保留两位小数正则

<el-input
type="text"
v-model.trim="ruleForm2.marketPrice"
maxlength="10"
@keyup.native="ruleForm2.marketPrice =ruleForm2.marketPrice = ruleForm2.marketPrice.replace(/[^\d.]/g,'');
ruleForm2.marketPrice = ruleForm2.marketPrice.replace(/\.{2,}/g,'.');
ruleForm2.marketPrice = ruleForm2.marketPrice.replace(/^\./g,'');
ruleForm2.marketPrice = ruleForm2.marketPrice.replace('.','$#$').replace(/\./g,'').replace('$#$','.');
ruleForm2.marketPrice = ruleForm2.marketPrice.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
"
></el-input>
 
 

function clearNoNum(obj)

{

//先把非数字的都替换掉,除了数字和.

obj.value = obj.value.replace(/[^\d.]/g,"");

//保证只有出现一个.而没有多个.

obj.value = obj.value.replace(/\.{2,}/g,".");

//必须保证第一个为数字而不是.

obj.value = obj.value.replace(/^\./g,"");

//保证.只出现一次,而不能出现两次以上

obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");

//只能输入两个小数

obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');

}

上一篇:jQuery mouseover与mouseenter,mouseout与mouseleave的区别


下一篇:理解mouseover,mouseout,mouseenter,mouseleave