关于vue.js:elementui中表单验证的三种方式

45次阅读

共计 2407 个字符,预计需要花费 7 分钟才能阅读完成。

  1. 表单加上 rules
<el-form class="apply-form first-form" :model="formData" :rules="rule" ref="form">
    <el-form-item label="姓名" prop="visitorName">
        <el-input v-model="formData.visitorName" placeholder="请输出姓名" clearable></el-input>
    </el-form-item>
    <el-form-item label="身份证号" prop="cardCode">
       <el-input v-model="formData.cardCode" :maxlength="18" placeholder="请输出身份证号" clearable></el-input>
    </el-form-item>
</el-form>

对应的 data() 里边写 rule{}

data() {
   return {
       formData: {
           visitorName: '',
           cardType: 1,
           cardCode: ''
       },
       rule: {
           visitorName: [{ required: true, message: '请输出姓名', trigger: 'blur'},
               {min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur'},
               {
                        required: true,
                        pattern: /^[\u4e00-\u9fa5_a-zA-Z0-9.·-]+$/,
                        message: '姓名不反对特殊字符',
                        trigger: 'blur'
               }
          ],
          cardCode: [{ required: true, message: '请输出身份证号', trigger: 'blur'},
              {min: 15, max: 18, message: '请如实填写 18 位号码,以供学校保卫科核查', trigger: 'blur'},
              {
                   required: true,
                   pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
                   message: '请输出正确的身份证号码',
                   trigger: 'blur'
              }
          ]
      }
   }
}

自定义校验规定:

data() {let reg = /(?!^(\d+|[a-zA-Z]+|[~!@#$%^&*?]+)$)^[\w~!@#$%^&*?]{6,12}$/
    var validateNewPwd = (rule, value, callback) => {if (!reg.test(value)) {callback(new Error('明码应是 6 -12 位数字、字母或字符!'))
        } else if (this.form.oldPasswd === value) {callback(new Error('新密码与旧明码不可统一!'))
        } else {callback()
        }
    }
    var validateComfirmPwd = (rule, value, callback) => {if (!reg.test(value)) {callback(new Error('明码应是 6 -12 位数字、字母或字符!'))
        } else if (this.form.newPasswd !== value) {callback(new Error('确认明码与新密码不统一!'))
        } else {callback()
        }
    }
    return {
        form: {
            newPasswd: '',
            comfirmPwd: ''
        },
        rules: {
            newPasswd: [{ required: true, message: '请输出新密码', trigger: 'blur'},
                {validator: validateNewPwd, trigger: 'blur'}
            ],
            comfirmPwd: [{ required: true, message: '请输出确认明码', trigger: 'blur'},
                {validator: validateComfirmPwd, trigger: 'blur'}
            ]
        }
    }
}
  1. 独自增加 relus
<el-form-item label="电话号码" :prop="phoneNum" :rules="[{required: true, message:' 请输出电话号码 ', trigger:'blur'}, {required: true, pattern: /^((13|14|15|16|17|18)[0-9]{1}\d{8})|((166|199|198)[0-9]{1}\d{7})$/, message:' 请输出正确的电话号码 ', trigger:'blur'}]">
    <el-input v-model="v.phoneNum" :maxlength="11" placeholder clearable></el-input>
</el-form-item>
  1. 动静增加表单项
addPhone() {
    let len = this.formData.phoneInfoList.length
    this.$set(this.formData.phoneInfoList, len, {relation: [],
        phoneNum: '',
        relationType: 0,
        schoolId: this.selectedUser.schoolId,
        userCode: this.selectedUser.userCode
    })
},
deletePhone(item) {if (this.formData.phoneInfoList.length > 1) {
        // 示意先获取这个元素的下标,而后从这个下标开始计算,删除长度为 1 的元素
        this.formData.phoneInfoList.splice(this.formData.phoneInfoList.indexOf(item), 1);
    } else {this.$vux.toast.text('至多保留一个家长亲情号码!')
        return false
    }
},


正文完
 0