vue中自定义rule进行校验input框只能为数字

vue

<el-input v-model="age" maxlength="3"/>

js

 const isNum = (rule, value, callback) => {
      const age= /^[0-9]*$/
      if (!age.test(value)) {
        callback(new Error('年龄只能为数字'))
      }else{
        callback()
      }
    }
rules: {
        age: [
          { required: true, message: '请输入年龄', trigger: 'blur' },
          { validator: isNum, trigger: 'blur' }
        ],

 

Logo

前往低代码交流专区

更多推荐