js密码强度

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>密码强度检测</title>
		<style type="text/css">
			body {
				font: 12px/1.5 Arial;
			}
			input {
				float: left;
				font-size: 12px;
				width: 150px;
				font-family: arial;
				border: 1px solid #ccc;
				padding: 3px;
			}
			input.correct {
				border: 1px solid green;
			}
			input.error {
				border: 1px solid red;
			}
			#tips {
				float: left;
				margin: 2px 0 0 20px;
			}
			#tips span {
				float: left;
				width: 50px;
				height: 20px;
				color: #fff;
				overflow: hidden;
				background: #ccc;
				margin-right: 2px;
				line-height: 20px;
				text-align: center;
			}
			#tips.s1 .active {
				background: #f30;
			}
			#tips.s2 .active {
				background: #fc0;
			}
			#tips.s3 .active {
				background: #cc0;
			}
			#tips.s4 .active {
				background: #090;
			}
		</style>
		<script type="text/javascript">
			window.onload = function() {
				var oTips = document.getElementById("tips");
				var oInput = document.getElementsByTagName("input")[0];
				var aSpan = oTips.getElementsByTagName("span");
				var aStr = ["弱", "中", "强", "非常好"];
				var i = 0;

				oInput.onkeyup = oInput.onfocus = oInput.onblur = function() {
					var index = checkStrong(this.value);
					this.className = index ? "correct" : "error";
					oTips.className = "s" + index;
					for ( i = 0; i < aSpan.length; i++)
						aSpan[i].className = aSpan[i].innerHTML = "";
						if(index==4){
							index && (aSpan[index - 1].className = "active", aSpan[index - 1].innerHTML = aStr[index - 1]);
							index && (aSpan[index - 2].className = "active", aSpan[index - 2].innerHTML = aStr[index - 2]);
							index && (aSpan[index - 3].className = "active", aSpan[index - 3].innerHTML = aStr[index - 3]);
							index && (aSpan[index - 4].className = "active", aSpan[index - 4].innerHTML = aStr[index - 4]);
						}else if(index==3){
							index && (aSpan[index - 1].className = "active", aSpan[index - 1].innerHTML = aStr[index - 1]);
							index && (aSpan[index - 2].className = "active", aSpan[index - 2].innerHTML = aStr[index - 2]);
							index && (aSpan[index - 3].className = "active", aSpan[index - 3].innerHTML = aStr[index - 3]);
						}else if(index==2){
							index && (aSpan[index - 1].className = "active", aSpan[index - 1].innerHTML = aStr[index - 1]);
							index && (aSpan[index - 2].className = "active", aSpan[index - 2].innerHTML = aStr[index - 2]);
						}else if(index==1){
							index && (aSpan[index - 1].className = "active", aSpan[index - 1].innerHTML = aStr[index - 1]);
						}
						
				};
			};
			/** 强度规则
			 + ------------------------------------------------------- +
			 1) 任何少于6个字符的组合,弱;任何字符数的同类字符组合,弱;
			 2) 任何字符数的两类字符组合,中;
			 3) 12位字符数以下的三类或四类字符组合,强;
			 4) 12位字符数以上的三类或四类字符组合,非常好。
			 + ------------------------------------------------------- +
			 **/
			//检测密码强度
			function checkStrong(sValue) {
				var modes = 0;
				if (sValue.length < 6)
					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;
						break;
					case 2:
						return 2;
					case 3:
					case 4:
						return sValue.length < 12 ? 3 : 4;
						break;
				}
			}
		</script>
	</head>
	<body>
		<input type="password" value="" maxlength="16" />
		<div id="tips">
			<span></span><span></span><span></span><span></span>
		</div>
	</body>
</html>

 

转载于:https://www.cnblogs.com/yuwensong/p/3747472.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 HTML、CSS 和 JavaScript 中实现密码强度校验可以通过以下步骤完成: 1. 在 HTML 中创建密码输入框和一个用于显示密码强度的元素(例如一个 `<span>` 或 `<div>` 元素)。例如: ``` <label for="password">密码:</label> <input type="password" id="password" name="password" onkeyup="checkPasswordStrength(this.value)"> <span id="password-strength"></span> ``` 在这个例子中,我们创建了一个密码输入框和一个用于显示密码强度的 `<span>` 元素。在输入框中输入密码时,会触发 `onkeyup` 事件,并且调用 `checkPasswordStrength()` 函数来计算密码强度并更新显示。 2. 在 CSS 中定义密码强度的样式。例如: ``` #password-strength { display: inline-block; width: 100px; height: 10px; margin-left: 10px; } #password-strength.weak { background-color: red; } #password-strength.medium { background-color: orange; } #password-strength.strong { background-color: green; } ``` 在这个例子中,我们定义了三个类名,分别对应于密码强度弱、中、强三种情况。在 `<span>` 元素中根据密码强度的不同,动态地添加或删除这些类名,从而改变元素的背景颜色。 3. 在 JavaScript 中编写 `checkPasswordStrength()` 函数来计算密码强度。例如: ``` function checkPasswordStrength(password) { var strength = 0; if (password.length >= 8) { strength += 1; } if (password.match(/[a-z]/)) { strength += 1; } if (password.match(/[A-Z]/)) { strength += 1; } if (password.match(/[0-9]/)) { strength += 1; } var passwordStrengthElement = document.getElementById('password-strength'); if (strength == 0) { passwordStrengthElement.className = ''; passwordStrengthElement.innerText = ''; } else if (strength == 1) { passwordStrengthElement.className = 'weak'; passwordStrengthElement.innerText = '弱'; } else if (strength == 2) { passwordStrengthElement.className = 'medium'; passwordStrengthElement.innerText = '中'; } else if (strength >= 3) { passwordStrengthElement.className = 'strong'; passwordStrengthElement.innerText = '强'; } } ``` 在这个例子中,我们定义了一个 `checkPasswordStrength()` 函数,并将其绑定到密码输入框的 `onkeyup` 事件上。函数中使用正则表达式来检查密码中是否包含大写字母、小写字母、数字等字符,然后根据各种条件来计算密码的强度,并更新密码强度元素的类名和文本内容。 这样,当用户在密码输入框中输入密码时,就会动态地显示密码的强度,并且根据强度的不同,密码强度元素的背景颜色也会发生变化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值