1、表单验证 v-model:
<a-form-model ref="form" :model="form" :rules="rules" >
<a-form-model-item
label="身份证号" name="idCard" prop="idCard"
:labelCol="{span: 5}"
:wrapperCol="{span: 19}"
>
<a-input v-model="form.idCard"/>
</a-form-model-item>
</a-form-model>
data(){
return{
rules:{
password: [
{
required: true, pattern: /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[~!@#$%^&*()_+`\-={}:";'<>?,.\/]).{8,}$/, message: '密码由8位数字、大小写字母和特殊符号组成!!'
}
],
idCard:[
{ required: true, message: '密码不能为空!'},
{ validator: this.validateidCard, trigger: 'change' }
],
}
}
}
validateidCard(rule, value, callback) {
console.log(value)
},
that.$refs['form'].validate(success => {
if (success === true) {
}
})
2、表单验证 v-decorator:
<a-form :form="form">
<a-form-item
label="身份证号"
:labelCol="{span: 5}"
:wrapperCol="{span: 19}"
>
<a-input
v-decorator.trim="['idCard',validatorRules.idCard]"
placeholder="请输入身份证号"
/>
</a-form-item>
</a-form>
data(){
return{
form:this.$form.createForm(this),
validatorRules: {
name:{
rules: [
{ required: true, message: '请输入代理人用户名!' }
]
},
idCard: {
rules: [
{ required: true, message: '' },
{ validator: this.validateidCard }
]
},
}
}
}
validateidCard(rule, value, callback) {
console.log(value)
}
this.form.setFieldsValue({
name: '设置值'
})
this.form.getFieldValue('name')
this.form.validateFields((err, values) => {
if (!err) {
}
})