jQuery实现密码强度检测,以破解时间来衡量(未测试)

[JavaScript]代码    

(function($){ 
     $.fn.extend({  
         pwdstr: function(el) {			
			return this.each(function() {
					
					
					
					$(this).keyup(function(){
						$(el).html(getTime($(this).val()));
					});
					
					function getTime(str){
					
					var chars = 0;
					var rate = 2800000000;
					
					if((/[a-z]/).test(str)) chars +=  26;
					if((/[A-Z]/).test(str)) chars +=  26;
					if((/[0-9]/).test(str)) chars +=  10;
					if((/[^a-zA-Z0-9]/).test(str)) chars +=  32;

					var pos = Math.pow(chars,str.length);
					var s = pos/rate;
					
					var decimalYears = s/(3600*24*365);
					var years = Math.floor(decimalYears);
					
					var decimalMonths =(decimalYears-years)*12;
					var months = Math.floor(decimalMonths);
					
					var decimalDays = (decimalMonths-months)*30;
					var days = Math.floor(decimalDays);
					
					var decimalHours = (decimalDays-days)*24;
					var hours = Math.floor(decimalHours);
					
					var decimalMinutes = (decimalHours-hours)*60;
					var minutes = Math.floor(decimalMinutes);
					
					var decimalSeconds = (decimalMinutes-minutes)*60;
					var seconds = Math.floor(decimalSeconds);
					
					var time = [];
					
					if(years > 0){
						if(years == 1)
							time.push("1 year, ");
						else
							time.push(years + " years, ");
					}
					if(months > 0){
						if(months == 1)
							time.push("1 month, ");
						else
							time.push(months + " months, ");
					}
					if(days > 0){
						if(days == 1)
							time.push("1 day, ");
				 		else
							time.push(days + " days, ");
					}
					if(hours > 0){
						if(hours == 1)
							time.push("1 hour, ");
						else
							time.push(hours + " hours, ");
					}
					if(minutes > 0){
						if(minutes == 1)
							time.push("1 minute, ");
						else
							time.push(minutes + " minutes, ");
					}
					if(seconds > 0){
						if(seconds == 1)
							time.push("1 second, ");
						else
							time.push(seconds + " seconds, ");
					}
					
					if(time.length <= 0)
						time = "less than one second, ";
					else if(time.length == 1)
						time = time[0];
					else
						time = time[0] + time[1];

					 return time.substring(0,time.length-2);
					}
					
			 });
        } 
    }); 
})(jQuery); 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基本的jQuery密码强度检测代码: ```javascript $(document).ready(function(){ $('#password').keyup(function(){ var password = $(this).val(); var strength = 0; if (password.match(/[a-z]+/)) { strength += 1; } if (password.match(/[A-Z]+/)) { strength += 1; } if (password.match(/[0-9]+/)) { strength += 1; } if (password.match(/[$@#&!]+/)) { strength += 1; } if (password.length >= 8) { strength += 1; } switch(strength) { case 0: $('#password-strength').html("密码强度:太弱了").css('color', 'red'); break; case 1: $('#password-strength').html("密码强度:弱").css('color', 'orange'); break; case 2: $('#password-strength').html("密码强度:一般").css('color', 'yellow'); break; case 3: $('#password-strength').html("密码强度:强").css('color', 'green'); break; case 4: $('#password-strength').html("密码强度:很强").css('color', 'darkgreen'); break; default: $('#password-strength').html(""); } }); }); ``` 在这个代码中,我们使用了jQuery的`keyup()`方法来检测密码输入框的内容是否发生了变化。然后我们获取密码的值,并使用正则表达式来检查密码是否包含小写字母、大写字母、数字和特殊字符以及密码的长度是否大于等于8个字符。每个条件都会增加密码的强度值。最后,我们使用`switch()`语句根据不同的强度值来显示不同的提示信息,并根据强度值来设置不同的颜色。 你可以根据自己的需求来自定义密码强度检测的条件和提示信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值