vue修改密码页面

<template>
    <el-form class="user-account-key" ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="原密码" prop="password">
            <el-input type="password" placeholder="请输入原密码" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
            <el-input type="password" placeholder="请设置新密码" v-model="form.newPassword"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="newPassword2">
            <el-input type="password" placeholder="请确认新密码" v-model="form.newPassword2"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit('form')">保存</el-button>
            <el-button @click="$refs['form'].resetFields()">重置</el-button>
        </el-form-item>
    </el-form>
</template>
<script>
    import {storePacks} from '@/contants'

    export default {
        data() {
            let validateNewPassword = (rule, value, callback) => {
                if (value === this.form.password) {
                    callback(new Error('新密码不能与原密码相同!'))
                } else {
                    callback()
                }
            }
            let validateNewPassword2 = (rule, value, callback) => {
                if (value !== this.form.newPassword) {
                    callback(new Error('与新密码不一致!'))
                } else {
                    callback()
                }
            }
            return {
                form: {},
                rules: {
                    password: [
                        {required: true, message: '请输入原密码', trigger: 'blur'}
                    ],
                    newPassword: [
                        {required: true, message: '请设置新密码', trigger: 'blur'},
                        {validator: validateNewPassword, trigger: 'blur'}
                    ],
                    newPassword2: [
                        {required: true, message: '请确认新密码', trigger: 'blur'},
                        {validator: validateNewPassword2, trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            onSubmit(formName) {
                const params = {
                    password : this.form.password ,
                    newPassword: this.form.newPassword,
                }
                this.$refs[formName].validate(valid => {
                    if (valid) {
                        this.$store.dispatch(`${storePacks.PASS_WORD}/changePass`, params)
                            .then((res) => {
                                this.$message.success('密码已修改')
                            })
                    } else {
                        this.$message.error('请正确填写表单')
                        return false
                    }
                })
            }
        },
    }
</script>

<style lang="scss">
    .user-account-key {
        width: 500px;
        margin: 0 auto;
    }
</style>

Vue 2中,如果创建一个用于修改密码的表单,表单验证通常会使用Vuelidate库或者直接在`v-model`指令的`rules`属性中设置校验规则。下面是一个基本示例: ```html <template> <form @submit.prevent="submitForm"> <input type="password" v-model="oldPassword" placeholder="旧密码" :rules="{ required: true, min: 6 }" /> <input type="password" v-model="newPassword" placeholder="新密码" :rules="{ required: true, min: 6, equal: 'oldPassword' }" /> <input type="password" v-model="confirmNewPassword" placeholder="确认新密码" :rules="{ required: true, min: 6, equal: 'newPassword' }" /> <button type="submit">提交</button> </form> </template> <script> import { required, minLength, sameAs } from "vuelidate/lib/validators"; export default { data() { return { oldPassword: "", newPassword: "", confirmPassword: "", }; }, validations: { form: { oldPassword: { required, minLength }, newPassword: { required, minLength, sameAs: "oldPassword" }, // 检查新密码是否与旧密码相同 confirmPassword: { required, minLength, sameAs: "newPassword" }, // 检查确认密码是否与新密码相同 }, }, methods: { submitForm() { this.$v.form.validate((valid) => { if (valid) { // 提交表单数据... } else { console.log("表单验证失败"); } }); }, }, }; </script> ``` 在这个例子中,我们设置了如下的验证规则: - `required`:密码字段不能为空。 - `minLength`:密码长度至少为6位。 - `sameAs`:检查新密码是否与指定的字段(这里是旧密码或确认新密码)一致。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值