随笔 - 20  文章 - 2  trackbacks - 0
<2009年3月>
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(1)

随笔档案

相册

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 

引用

邵波的空间JavaScript密码强度检测源代码
1.Body代码部分
  1. <body>
  2.  <h4>密码强度检测</h4>
  3.  <table width="100%" border="0" cellspacing="1" cellpadding="0">
  4.  <tr>
  5.  <td width="100" align="right">强度显示:</td>
  6.  <td>
  7.  <script language="javascript">
  8. var ps = new PasswordStrength();
  9. ps.setSize("200","20");
  10. ps.setMinLength(5);
  11.  </script>
  12.  </td>
  13.  </tr>
  14.  <tr>
  15.  <td align="right">密码检测:</td>
  16.  <td><input name="pwd" type="password" id="pwd" style="width:200px" onKeyUp="ps.update(this.value);"></td>
  17.  </tr>
  18.  </table>
  19.  </body>

2.JS代码部分

  1. //密码强度;
  2.  function PasswordStrength(showed){
  3.  this.showed = (typeof(showed) == "boolean")?showed:true;
  4.  this.styles = new Array();
  5.  this.styles[0] = {backgroundColor:"#EBEBEB",borderLeft:"solid 1px #FFFFFF",borderRight:"solid 1px #BEBEBE",borderBottom:"solid 1px #BEBEBE"};
  6.  this.styles[1] = {backgroundColor:"#FF4545",borderLeft:"solid 1px #FFFFFF",borderRight:"solid 1px #BB2B2B",borderBottom:"solid 1px #BB2B2B"};
  7.  this.styles[2] = {backgroundColor:"#FFD35E",borderLeft:"solid 1px #FFFFFF",borderRight:"solid 1px #E9AE10",borderBottom:"solid 1px #E9AE10"};
  8.  this.styles[3] = {backgroundColor:"#95EB81",borderLeft:"solid 1px #FFFFFF",borderRight:"solid 1px #3BBC1B",borderBottom:"solid 1px #3BBC1B"};
  9.  
  10.  this.labels= ["弱","中","强"];
  11.  
  12.  this.divName = "pwd_div_"+Math.ceil(Math.random()*100000);
  13.  this.minLen = 5;
  14.  
  15.  this.width = "150px";
  16.  this.height = "16px";
  17.  
  18.  this.content = "";
  19.  
  20.  this.selectedIndex = 0;
  21.  
  22.  this.init();
  23.  }
  24.  PasswordStrength.prototype.init = function(){
  25.  var s = '<table cellpadding="0" id="'+this.divName+'_table" cellspacing="0" style="width:'+this.width+';height:'+this.height+';">';
  26.  s += '<tr>';
  27.  for(var i=0;i<3;i++){
  28.  s += '<td id="'+this.divName+'_td_'+i+'" width="33%" align="center"><span style="font-size:1px"> </span><span id="'+this.divName+'_label_'+i+'" style="display:none;font-family: Courier New, Courier, mono;font-size: 12px;color: #000000;">'+this.labels[i]+'</span></td>';
  29.  }
  30.  s += '</tr>';
  31.  s += '</table>';
  32.  this.content = s;
  33.  if(this.showed){
  34.  document.write(s);
  35.  this.copyToStyle(this.selectedIndex);
  36.  }
  37.  }
  38.  PasswordStrength.prototype.copyToObject = function(o1,o2){
  39.  for(var i in o1){
  40.  o2[i] = o1[i];
  41.  }
  42.  }
  43.  PasswordStrength.prototype.copyToStyle = function(id){
  44.  this.selectedIndex = id;
  45.  for(var i=0;i<3;i++){
  46.  if(i == id-1){
  47.  this.$(this.divName+"_label_"+i).style.display = "inline";
  48.  }else{
  49.  this.$(this.divName+"_label_"+i).style.display = "none";
  50.  }
  51.  }
  52.  for(var i=0;i<id;i++){
  53.  this.copyToObject(this.styles[id],this.$(this.divName+"_td_"+i).style);
  54.  }
  55.  for(;i<3;i++){
  56.  this.copyToObject(this.styles[0],this.$(this.divName+"_td_"+i).style);
  57.  }
  58.  }
  59.  PasswordStrength.prototype.$ = function(s){
  60.  return document.getElementById(s);
  61.  }
  62.  PasswordStrength.prototype.setSize = function(w,h){
  63.  this.width = w;
  64.  this.height = h;
  65.  }
  66.  PasswordStrength.prototype.setMinLength = function(n){
  67.  if(isNaN(n)){
  68.  return ;
  69.  }
  70.  n = Number(n);
  71.  if(n>1){
  72.  this.minLength = n;
  73.  }
  74.  }
  75.  PasswordStrength.prototype.setStyles = function(){
  76.  if(arguments.length == 0){
  77.  return ;
  78.  }
  79.  for(var i=0;i<arguments.length && i < 4;i++){
  80.  this.styles[i] = arguments[i];
  81.  }
  82.  this.copyToStyle(this.selectedIndex);
  83.  }
  84.  PasswordStrength.prototype.write = function(s){
  85.  if(this.showed){
  86.  return ;
  87.  }
  88.  var n = (s == 'string') ? this.$(s) : s;
  89.  if(typeof(n) != "object"){
  90.  return ;
  91.  }
  92.  n.innerHTML = this.content;
  93.  this.copyToStyle(this.selectedIndex);
  94.  }
  95.  PasswordStrength.prototype.update = function(s){
  96.  if(s.length < this.minLen){
  97.  this.copyToStyle(0);
  98.  return;
  99.  }
  100.  var ls = -1;
  101.  if (s.match(/[a-z]/ig)){
  102.  ls++;
  103.  }
  104.  if (s.match(/[0-9]/ig)){
  105.  ls++;
  106.  }
  107.  if (s.match(/(.[^a-z0-9])/ig)){
  108.  ls++;
  109.  }
  110.  if (s.length < 6 && ls > 0){
  111.  ls--;
  112.  }
  113.  switch(ls) {
  114.  case 0:
  115.  this.copyToStyle(1);
  116.  break;
  117.  case 1:
  118.  this.copyToStyle(2);
  119.  break;
  120.  case 2:
  121.  this.copyToStyle(3);
  122.  break;
  123.  default:
  124.  this.copyToStyle(0);
  125.  }
  126.  }

文章来源:http://wxq594808632.blog.163.com/blog/static/10907975520092191710543
posted on 2009-03-19 13:07 武志强 阅读(93) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: