form表单的校验
一:常见的表单校验
1.data
// 单位信息
info: {},
// 表单校验规则
formValidators: {
name: [{ required: true, message: '请输入单位名称' }],
phone: [{ validator: this.validatePhone }],
email: [{ validator: this.validateEmail }],
},
2.methods
// 提交
submit () {
this.$refs.drawerForm.validate((valid) => {
if (valid) {
if (!this.externalId) {
this.addExternal() // 新增
} else {
this.editExternal() // 编辑
}
}
})
},
// 校验电话
validatePhone (rule, value, callback) {
if (value) {
const tel = /^0\d{2,3}-?\d{7,8}$/
const phone = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/
if (value.length === 11) { // 手机号码
if (phone.test(value)) {
callback()
}
} else if (value.length === 13 && value.in