jQuery密码强度检查器 [英] jQuery password strength checker

查看:114
本文介绍了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密码强度检查器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆