一段基于jQuery的,验证用户密码的简单应用2

// check the result generally
var ip = new RegExp(/^0[^71]|^11|^10|^19|^18|^17|120/);
var noVal = "";
// message for notice
var msgMap = {
"^0":"  请输入原有密码",
"^11":"  新密码复杂度不够",
"^10":"  新密码过短",
"^17":"  请输入新密码",
"^19":"  新密码过长",
"^18":"  新密码包括非法字符",
"120":"  密码重复错误"
};
// find the input text
var pMap = {
"^0":"pw2",
"^11":"pw0",
"^10":"pw0",
"^17":"pw0",
"^19":"pw0",
"^18":"pw0",
"120":"pw1"
};
// initiate
function initP(){
$("#modPwd").click(function(){
		var display = $("#inputPwd").css("display");
		$("#inputPwd").css("display",display =="none"?"":"none");
	});
$("input[type='submit']").click(function(){
		return sC();//submit check
	});
	iC();//input check
}
//check for submit
function sC(){
var ps = getApw();
var r = aop(ps[2])+""+checkPass(ps[0])+""+arp(ps[0],ps[1]);
//if (aop(ps[2]) == 1 && (ps[0] == noVal || ps[1] == noVal)) {alert("请输入新密码");return false;}
//alert(r);
if (r.match(ip))
{
	for(var k in msgMap){
		if (r.match(k))
		{
			alert(msgMap[k]);
			try
			{
			$("#"+pMap[k]).select();
			return false;
			break;
			}
			catch (e)
			{}
		}
	}
} 
}
// get all password
function getApw(){
var ps = new Array();
		$("input[id^='pw']").each(function(e){
			var i = this.id.replace('pw','');
			ps[i] = this.value;
		});
return ps;
}

// check for input
function iC(){
var n = document.createElement("span");//create the element for notice
$(n).css({color:'red'});
$("input[id^='pw']").keyup(function(){
	var ps = getApw();
	var r = aop(ps[2])+""+checkPass(ps[0])+""+arp(ps[0],ps[1]);
	//alert(r);
	if (r.match(ip))
		{
			for(var k in msgMap){
				if (r.match(k))
				{
					n.innerText = msgMap[k];
					$("#"+pMap[k]).after(n);// display the notice
					break;
				}
			}
		} 
	else { n.innerText = "";}
});
}
// check the repeat password
function arp(np,rp){
if (np == rp) return 1;
else return 0;
}
// check the old password
function aop(op){
if (op == noVal)return 0;
else return 1;
}
// clean all password input text
function clean(){
var ps = getApw();
for (var i = 0;i < ps.length ;i++ )
{
	ps[i].value="";
}
}

 html文件中的form表单

<form id="pw">   
ORIGINAL PASSWORD:<input type="text"  id="pw0"/>   
<br><br>   
NEW PASSWORD:<input type="text"  id="pw1"/>   
<br><br>   
REPEAT PASSWORD:<input type="text" id="pw2"/>   
<br><br>   
<input type="submit" value="submit"/>   
</form>   

 此篇文章仍为原创,上一次的密码验证在使用中出现了不少问题,所以进行了重写,这回的代码和上次相比略有清爽感,绝的作为程序员代码重构还是很重要的,希望能有读者给予评论,哈哈。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值