vue:el-input输入时限制输入的类型

本文介绍在Vue.js中如何使用@keyup.native监听器动态监控输入类型,包括手机号码和身份证号码的格式验证,以及如何限制表格输入的数字长度。通过split方法和this.$set更新动态创建的字段,确保数据的正确性和格式一致性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过@keyup.native的时间动态监控输入的类型
1.手机号码,只能是数字,如果输入了非数字直接清空
2.身份证号码,除了Xx和数字其余的一律清空
3.基于1.2两种情况下,还有一种是动态创建的字段(也就是v-for出来的),解决方法:先使用split形成字段数组,使用for循环找到最后一个点的前面的字段,方便使用$set更新和渲染页面

    setDelMsicStr(field,type){
      let props
      let len
      let value
      let newphoestr
      let item = this
      if (field) {
        props = field.split('.')
        len = props.length
        for (let i = 0; i < len - 1; i++) {
          item = item[props[i]]
        }
        if(type=="phone"){
          newphoestr = (item[props[len - 1]]).replace(/([^0-9])+/g, '')
        }else if(type=='idCard'){
          newphoestr = (item[props[len - 1]]).replace(/([^0-9Xx])+/g, '')
        }
        this.$set(item, props[len - 1], newphoestr)
      }
    },

重点:也是使用this.$set()时必须的点

        for (let i = 0; i < len - 1; i++) {
          item = item[props[i]]
        }

表格限制输入的数字长度,超过限定值,直接显示9999

                    <el-form-item prop="activStoreSellPrice">
                      <el-input type="number" @keyup.native="setRange('form.prdctStoreList.'+scope.$index+'.activStoreSellPrice',99999,0)" v-model.number="scope.row.activStoreSellPrice" :disabled="disabled" min="0" max="99999999"></el-input>
                    </el-form-item>

重点:
表格的需要获取到行的index(scope.$index)

@keyup.native="setRange('form.prdctStoreList.'+scope.$index+'.activStoreSellPrice',99999,0)"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值