vue表单中加入密码强度的样式

在这里插入图片描述

<el-form-item label="密码强度:">
                            <div class="input_span">
                                <span id="one"/>
                                <span id="two"/>
                                <span id="three"/>
                            </div>
                            <div id="font">
                                <span></span>
                                <span></span>
                                <span></span>
                            </div>
                        </el-form-item>

// data msgText: '',

watch: {
            'passwordForm.newOne': {
                handler(newname, oldname) {
                    this.msgText = this.checkStrong(newname);
                    console.log(this.msgText, '=====');
                    if (this.msgText > 1 || this.msgText === 1) {
                        document.getElementById('one').style.background = 'red';
                    } else {
                        document.getElementById('one').style.background = '#eee';
                    }
                    if (this.msgText > 2 || this.msgText === 2) {
                        document.getElementById('two').style.background = 'orange';
                    } else {
                        document.getElementById('two').style.background = '#eee';
                    }
                    if (this.msgText === 4) {
                        document.getElementById('three').style.background = '#00D1B2';
                    } else {
                        document.getElementById('three').style.background = '#eee';
                    }
                }
            }
        },


// methods
checkStrong(sValue) {
                var modes = 0;
                // 正则表达式验证符合要求的
                if (sValue.length < 1) { return modes; }
                if (/\d/.test(sValue)) { modes++; } // 数字
                if (/[a-z]/.test(sValue)) { modes++; } // 小写
                if (/[A-Z]/.test(sValue)) { modes++; } // 大写
                if (/\W/.test(sValue)) { modes++; } // 特殊字符
                switch (modes) {
                    case 1:
                        return 1;
                    case 2:
                        return 2;
                    case 3:
                    case 4:
                        return sValue.length < 4 ? 3 : 4;
                }
                return modes;
            }






.input_span {
    span {
        display: inline-block;
        width: 50px;
        height: 10px;
        border: 1px solid #ccc;
        &:first-child {
            border-right: 0;
            border-radius: 5px 0 0 5px;
        }
        &:last-child {
            border-left: 0;
            border-radius: 0 5px 5px 0;
        }
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js 是一个用于构建交互式 Web 界面的渐进式 JavaScript 框架。如果你想要在 Vue.js 实现密码强度验证,可以使用一个密码强度评估库来计算密码的强度,并使用 Vue.js 来更新界面。 一个流行的密码强度评估库是 zxcvbn,它可以计算密码的熵值,并根据熵值给出密码的强度评分。你可以使用 npm 安装 zxcvbn: ``` npm install zxcvbn ``` 在 Vue.js ,你可以在模板使用 `v-model` 绑定输入框密码,并使用 `watch` 监听密码的变化。当密码发生变化时,你可以使用 zxcvbn 计算密码的强度,并更新界面。 下面是一个简单的 Vue.js 组件示例,用于实现密码强度验证: ```html <template> <div> <label>Password:</label> <input type="password" v-model="password"> <p v-if="passwordStrength.score >= 0"> Password strength: {{ passwordStrength.score }} ({{ passwordStrength.feedback.warning }}) </p> </div> </template> <script> import zxcvbn from 'zxcvbn'; export default { data() { return { password: '', passwordStrength: { score: -1, feedback: {} } }; }, watch: { password: function() { this.passwordStrength = zxcvbn(this.password); } } }; </script> ``` 在这个示例,我们在模板使用 `v-model` 绑定输入框密码,并在 `watch` 监听密码的变化。当密码发生变化时,我们使用 zxcvbn 计算密码的强度,并将结果存储在 `passwordStrength` 对象。然后,我们在模板使用 `v-if` 来显示密码强度评分和反馈。 注意,这个示例并没有实现密码强度要求的验证逻辑。如果你需要在密码不符合要求时显示错误消息,你可以添加一些额外的代码来实现这个逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值