jQuery 密码强度检查器

jQuery password strength checker(jQuery 密码强度检查器)
本文介绍了jQuery 密码强度检查器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 jQuery 的新手,我编写了一个简单的函数来检查每次按键的密码强度.

I'm quite new to jQuery, and I've written a simple function to check the strength of a password for each keypress.

这个想法是,每次用户输入一个字符时,都会评估内容以测试他们输入的密码的强度......我相信每个人都以前见过这些.

The idea is that every time a user enters a character, the contents is evaluated to test the strengh of the password they have entered... I'm sure everyone has seen these before.

无论如何,我使用的逻辑是没有密码以值 1 开头.当使用小写字符时,分数增加到 2.当使用数字时,分数再次增加 1,同样当使用大写字符并且密码长度为 5 个或更多字符时.

Anyhow, the logic I have used is that no password begins with a value of 1. When a lower-case character is used, the score increments to 2. When a digit is used the score increments by 1 again, same for when an uppercase character is used and when the password becomes 5 or more characters long.

每次按下一个键时,返回的都是密码强度,从 1 到 5 的值.

What is returned is the strength of the password so far as a value from 1 to 5 every time a key is pressed.

那么,关于我的问题.我完成它的方式似乎不像 jQuery 那样......几乎就像我可能刚刚完成了直接的 javascript.我也想知道我的逻辑.我做了什么或忽略了什么?比我更聪明的人有什么建议吗?

So, about my question. The way that I've done it doesn't seem very jQuery like... almost like I may as well have just done straight javascript. Also I was wondering about my logic. Have I done anything done or overlooked something? Any suggestions from smarter people than myself?

任何建议或意见将不胜感激.

Any suggestions or advice would be appreciated.

$(document).ready(function(){

        $("#pass_strength").keyup(function() {

            var strength = 1;

            /*length 5 characters or more*/
            if(this.value.length >= 5) {
                strength++;
            }

            /*contains lowercase characters*/
            if(this.value.match(/[a-z]+/)) {
                strength++;
            }

            /*contains digits*/
            if(this.value.match(/[0-9]+/)) {
                strength++;
            }

            /*contains uppercase characters*/
            if(this.value.match(/[A-Z]+/)) {
                strength++;
            }

            alert(strength);
        });
     });

推荐答案

最好的方法是按照 TJB 的建议使用现有的插件.

The best way is to take an existing plugin as TJB suggested.

关于代码本身的问题,一个更好的方法是这样写:

As to your question about the code itself, a nicer way is to write it like that:

var pass = "f00Bar!";

var strength = 1;
var arr = [/.{5,}/, /[a-z]+/, /[0-9]+/, /[A-Z]+/];
jQuery.map(arr, function(regexp) {
  if(pass.match(regexp))
     strength++;
});

(已修改以纠正语法错误.)

(Modified to correct syntax errors.)

这篇关于jQuery 密码强度检查器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)