|
<html><head><title>PASSWORD CHECK</title><script language="javascript" type="text/javascript" src="../jquery/jquery.js"></script><script language="javascript" type="text/javascript">$(document).ready(function(){ initP();});// check the result generallyvar ip = new RegExp(/[0]+|111|121/);// message for noticevar msgMap = {"^0":"original password is wrong","^11|^12":"new password is too simple","^10":"new password is too short","^19":"new password is too long","130":"repeat password is wrong"};// find the input textvar pMap = {"^0":"pw0","^11|^12":"pw1","^10":"pw1","^19":"pw1","130":"pw2"};// initiatefunction initP(){$("input[type='submit']").click(function(){ return sC();//submit check }); iC();//input check}//check for submitfunction sC(){ var ps = getApw(); var r = aop(ps[0].value)+""+anp(ps[1].value)+""+arp(ps[1].value,ps[2].value); if (r.match(ip)) { for(var k in msgMap){ if (r.match(k)) { alert(msgMap[k]); try {$("#"+pMap[k]).select();} catch (e) {} return false; } } } }// get all passwordfunction getApw(){ var ps = new Array(); $("input[id^='pw']").each(function(e){ var i = this.id.replace('pw',''); ps[i] = this; }); return ps;}// check for inputfunction iC(){ var n = document.createElement("span");//create the element for notice $("input[id^='pw']").keyup(function(){ var ps = getApw(); var r = aop(ps[0].value)+""+anp(ps[1].value)+""+arp(ps[1].value,ps[2].value); if (r.match(ip)) { for(var k in msgMap){ if (r.match(k)) { n.innerText = msgMap[k]; $("#"+pMap[k]).after(n);// display the notice } } } else n.innerText = ""; });}// check the new passwordfunction anp(p){ if (p.length < 8) return 0;// it is too short if (p.length > 16) return 9;// it is too long var ls = 0; if (p.match(/([0-9])+/)) {ls++;} if (p.match(/([a-z])+/)) {ls++;} if (p.match(/([A-Z])+/)) {ls++;} if (p.match(/([^0-9a-zA-Z])+/)) {ls++;} if (ls >= 3) return 3;// it is ok return ls;//it is too simple}// check the repeat passwordfunction arp(np,rp){ if (np == rp) return 1; else return 0;}// check the original passwordfunction aop(p){ var op = 1; if (p == op) return 1; else return 0;}// clean all password input textfunction clean(){ var ps = getApw(); for (var i = 0;i < ps.length ;i++ ) { ps[i].value=""; }}</script></head><body>DEMO:<br><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></body></html> 我想根据html的内容基本能明白id和输入的对应关系,我是根据对原有密码,新密码和重复密码的验证结果集合成一个结果进行统一的验证,并不是分开校验,所以可能对理解代码有一点儿障碍,当然这么写也是存在实验性的了,恩,能力非常非常有限,还希望各位同学给予批评和指正,在下感激万分。 |
|