async-validator icon indicating copy to clipboard operation
async-validator copied to clipboard

number 数值的验证有问题吧

Open jiankafei opened this issue 6 years ago • 7 comments

input.value 都是 string 类型吧,直接 typeof 判断是不是number 有问题啊

jiankafei avatar Mar 25 '19 04:03 jiankafei

  • 1

luocong2016 avatar Mar 29 '19 09:03 luocong2016

<el-form-item
    label="年龄"
    prop="age"
    :rules="[
      { required: true, message: '年龄不能为空'},
      { type: 'number', message: '年龄必须为数字值'}
    ]"
  >
    <el-input type="age" v-model.number="sizeForm.age" autocomplete="off"></el-input>
  </el-form-item>

这个情况下是有问题

luocong2016 avatar Mar 29 '19 09:03 luocong2016

@luocong2016 v-model.number 这种写法已经不再支持了,所以我改用 type="number" 但这样就会有上述我说的问题

jiankafei avatar Mar 29 '19 09:03 jiankafei

我报的是 age 不是一个string 类型。 你看我代码设置type == 'number' 啊。压根没有string

luocong2016 avatar Apr 01 '19 01:04 luocong2016

这样就正常,我有使用 fullField 字段 <el-form-item label="年龄" prop="age" :rules=" { required: true, type: 'number' ]"

<el-input type="age" v-model.number="sizeForm.age" autocomplete="off"></el-input>

luocong2016 avatar Apr 01 '19 02:04 luocong2016

@luocong2016 先去掉 v-model.number 的number修饰符,然后使用 pattern 字段使用正则做判断吧,他们的正则只支持 正则实例和正则实例里的字符串正则参数两种方式 ,不支持字面量正则写法

jiankafei avatar Apr 01 '19 02:04 jiankafei

直接写有问题的,可以这样写,type类型默认类型,pattern:/\d/;用正则验证一下,就行了

gryns avatar Oct 15 '19 06:10 gryns