el-input 限制只能输入数字类型

只能输入数字类型

提示:使用正则过滤输入值

方法一

输入不符合字符后,不清空原来数字

<template>
	<el-input 
		  v-model.number="nCapacity"
          onkeyup="value=value.replace(/^\.+|[^\d.]/g,'')"
          @input="fnShareCapacityInput">
	</el-input>
</template>
<script>
export default {
	data() {
		return {
			nCapacity: 0
		}
	},
	fnShareCapacityInput() {
	 	// 增加限制
		if (event.target.value === '') {
            this.nCapacity = 0;
        } else if (event.target.value > 5000) {
            this.nCapacity = 5000;
        } else if (event.target.value < 1) {
            this.nCapacity = 0;
        }
	}
}
</script>
方法二

输入不符合字符后,清空原来数字

<template>
	<el-input
      v-model="num"
      oninput="if(value === ''){value = 0} if (isNaN(value)) {value = null}if (value.indexOf('.') > 0) {value = value.slice(0, value.indexOf('.'))} if(value > 1000000) value = 1000000"
      maxLength="1000000"
    ></el-input>
</template>

只能输入数字类型,包括小数点、负数

提示:限制奇怪的输入:

<template>
	<el-input
	  v-model="nZoomInput"
	  @input="fnZoomInputBox"
	>
	</el-input>
</template>
<script>
export default {
	data() {
		return {
			nZoomInput: 1
		}
	},
	methods: {
		// 输入框修改 缩放
	    fnZoomInputBox(val) {
	      let str = val.charAt(0), sInput;
	      sInput = val.replace(/[^0-9.]/g, "");
	      if (str === "-") sInput = "-" + sInput;
	      sInput = this.fnRegUnified(sInput);
	      if (sInput === "" || sInput < 0) {
	        sInput = 0;
	      } else if (sInput > 8) {
	        sInput = 8;
	      }
	      this.nZoomInput  = sInput;
	    },
	    // 统一正则判断
	    fnRegUnified(val) {
	      val = val.replace(/^00/, "0."); //开头不能有两个0
	      val = val.replace(/^\./g, "0."); //开头为小数点转换为0.
	      val = val.replace(/\.{2,}/g, "."); //两个以上的小数点转换成一个
	      val = val.replace(".", "$#$").replace(/\./g, "").replace("$#$", "."); //只保留一个小数点
	      val = val.replace(new RegExp("^(\\d+)\\.(\\d{2}).*$"), "$1.$2"); // 保留小数点后两位数
	      return val;
	    },
	}
}
</script>

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

忍冬 ⁡⁡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值