javascript 按位或(|),无符号右移(>>>)运算,组合技巧来实现————密码强度提示,四种情况??
直接上代码,原来的代码中,switch中的第一个case,判断之后,少加了个break 跳出判断语句,害得我查了半天,“怎么样式老是不对,不科学啊,呵呵,原来是没跳出case的判断了,还会执行后面的判断!!,哎,嘿嘿,不过后来还是发现了,开心中。。。”
原文地址:http://www.cnblogs.com/wybztn/archive/2009/11/18/1605285.html
这里还有个重要的设计技巧, 0001, 0010, 0100, 1000各代表一种情况的话,组合起来就有很多种情况,我们怎么来表示,或者说怎么来存储这n多种组合的情况呢,就用到了一个技巧,1.先按位或存储到一个变量a中, 2.再将 a & 1, 并无符号右移>>> ,直到为0 为止(停止)。
直接上代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | <html> <head> <meta http-equiv= "content-type" content= "text/html charset=utf-8" /> <title>Javascript评估用户输入密码的强度</title> <script language= "JavaScript" > //CharMode函数 //测试某个字符是属于哪一类. function CharMode(iN){ if (iN>=48 && iN <=57) //数字 return 1; if (iN>=65 && iN <=90) //大写字母 return 2; if (iN>=97 && iN <=122) //小写 return 4; else return 8; //特殊字符 } //bitTotal函数 //计算出当前密码当中一共有多少种模式 function bitTotal(num){ modes=0; for (i=0;i<4;i++){ if (num & 1) modes++; num>>>=1; } return modes; } //checkStrong函数 //返回密码的强度级别 function checkStrong(sPW){ if (sPW.length<=4) return 0; //密码太短 Modes=0; for (i=0;i<sPW.length;i++){ //测试每一个字符的类别并统计一共有多少种模式. Modes |=CharMode(sPW.charCodeAt(i)); } return bitTotal(Modes); } //pwStrength函数 //当用户放开键盘或密码输入框失去焦点时,根据不同的级别显示不同的颜色 function pwStrength(pwd){ O_color= "#eeeeee" ; L_color= "#FF0000" ; M_color= "#FF9900" ; H_color= "#33CC00" ; if (pwd== null ||pwd== '' ){ Lcolor=Mcolor=Hcolor=O_color; } else { S_level=checkStrong(pwd); switch (S_level) { case 0: Lcolor=Mcolor=Hcolor=O_color; break ; case 1: Lcolor=L_color; Mcolor=Hcolor=O_color; break ; case 2: Lcolor=Mcolor=M_color; Hcolor=O_color; break ; default : Lcolor=Mcolor=Hcolor=H_color; } } document.getElementById( "strength_L" ).style.background=Lcolor; document.getElementById( "strength_M" ).style.background=Mcolor; document.getElementById( "strength_H" ).style.background=Hcolor; return ; } </script> </head> <body> <form name=form1 action= "" > 输入密码:<input type=password size=10 onKeyUp=pwStrength( this .value) onBlur=pwStrength( this .value)> <br>密码强度: <table width= "217" border= "1" cellspacing= "0" cellpadding= "1" bordercolor= "#cccccc" height= "23" style= 'display:inline' > <tr align= "center" bgcolor= "#eeeeee" > <td width= "33%" id= "strength_L" >弱</td> <td width= "33%" id= "strength_M" >中</td> <td width= "33%" id= "strength_H" >强</td> </tr> </table> </form> </body> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现