2024年最新vue+elementUI用户修改密码的前端验证_element-ui 修改密码验证,web前端开发自学网
<template>
<el-form :model="form" :rules="rules" ref="form" label-width="100px">
<el-form-item label="原密码" prop="oldPassword">
<el-input type="password" v-model="form.oldPassword"></el-input>
</el-form-item>
<el-form-item label="新密码" prop="newPassword">
<el-input type="password" v-model="form.newPassword"></el-input>
</el-form-item>
<el-form-item label="确认密码" prop="confirmPassword">
<el-input type="password" v-model="form.confirmPassword"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm">提交</el-button>
</el-form-item>
</el-form>
</template>
<script>
export default {
data() {
let validatePass = (rule, value, callback) => {
if (value.length < 6) {
callback(new Error('密码不能小于6位'));
} else {
callback();
}
};
let validatePass2 = (rule, value, callback) => {
if (value !== this.form.newPassword) {
callback(new Error('两次输入密码不一致!'));
} else {
callback();
}
};
return {
form: {
oldPassword: '',
newPassword: '',
confirmPassword: ''
},
rules: {
oldPassword: [
{ required: true, message: '请输入原密码', trigger: 'blur' },
{ min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
],
newPassword: [
{ required: true, validator: validatePass, trigger: 'blur' }
],
confirmPassword: [
{ required: true, validator: validatePass2, trigger: 'blur' }
]
}
};
},
methods: {
submitForm() {
this.$refs.form.validate(valid => {
if (valid) {
alert('submit!');
} else {
console.log('error submit!!');
return false;
}
});
}
}
};
</script>
这段代码使用Vue和Element UI创建了一个修改密码的表单,包含了原密码、新密码和确认密码三个输入框。表单验证了密码的长度和一致性。当所有的验证通过后,会触发提交操作。这是一个典型的Web前端表单验证的例子,适用于教育用途。
评论已关闭