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() } }
vue-js
rules: { age: [ { required: true, message: '请输入年龄', trigger: 'blur' }, { validator: isNum, trigger: 'blur' } ],