element-rules
rules: { expiryOnDate: [ { validator: (rule, value, callback) => { if (!this.item.expiryControlType) { if (this.item.expiryOnDate == null) { callback( new Error(this.l("ThisFieldIsRequired")) ); } } callback(); } } ] },
第二种:
contactNumber: [{ required: true, validator: this.validMobile, tirgger: 'blur' }]
methods:{
validMobile(rule, value, callback) {
if (!this.form.contactNumber) {
callback(new Error('请输入手机号码'))
} else if (!isMobile(this.form.contactNumber)) {
callback(new Error('手机号码格式不正确'))
} else {
callback()
}
},
}
第二种 var legPhone = (rule, value, callback) => { var stalltest = /^\d{11}$/ // 配置校验为11位数字 var stallstate = stalltest.test(value) setTimeout(() => { if (!stallstate && value) { callback(new Error('请输入正确的手机号码')) } else { callback() } }, 50) } const initRules = () => ({ legPhone: [{ required: true, message: '请填写内容', trigger: 'blur' }, { required: true, validator: legPhone, trigger: 'blur' }], }) export default { components: { PhotoPreview }, data() { return { rules: initRules(), } } }