element-ui表单验证rules 其实在element-ui官⽅⽂档上,关于rules的⽤法已经讲得很详细了。以下是例⼦代码:
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm"> <el-form-item label="活动名称" prop="name">
<el-input v-model="ruleForm.name"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm('ruleForm')">⽴即创建</el-button>
</el-form-item>
</el-form>
<script>
export default {
data() {
var validatePass = (rule, value, callback) => {
if (value === '') {
callback(new Error('请输⼊密码'));
} el {
callback();
}
};
return {
ruleForm: {
name: '',
王者荣耀黄忠
},
rules: {
凯仕乐教的多音字//失去焦点时验证丑人
name: [
{ required: true, message: '请输⼊活动名称', trigger: 'blur' },
{ min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
//可以是函数验证
{ validator: validatePass, trigger: 'blur' }
//正则表达式验证
],
};
},
methods: {
submitForm(formName) {
//提交时的验证
this.$refs[formName].validate((valid) => {
刘丽莎if (valid) {
火张惠妹alert('submit!');
} el {
console.log('error submit!!');
return fal;
法制宣传日}
});
},
}
}
</script>
韩女主播