vant2 自动检查表单验证 -validate
ref 给
<van-form @submit="onSubmit" ref="form"> 标签 ;
// 检验手机号是否合格 await this.$refs.form.validate("mobile");
data里面定义的规则 rules对象 ;
rules: { mobile: [ // 必填 { required: true, message: "请填写用户名" }, // 1 3-9 开头 在家 9 个0-9数字 { pattern: /^1[3-9]\d{9}$/, message: "手机号不正确" }, ], code: [ { required: true, message: "请填写密码" }, { pattern: /\d{6}$/, message: "验证码错误" }, ], },
使用规则:
<van-field v-model="user.mobile" name="mobile" icon-prefix="zlx" left-icon="shouji" maxlength="11" placeholder="请输入手机号" :rules="rules.mobile" />