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

          常用鏈接

          留言簿(1)

          隨筆檔案

          相冊

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

           

          引用

          邵波的空間JavaScript密碼強(qiáng)度檢測源代碼
          1.Body代碼部分
          1. <body>
          2.  <h4>密碼強(qiáng)度檢測</h4>
          3.  <table width="100%" border="0" cellspacing="1" cellpadding="0">
          4.  <tr>
          5.  <td width="100" align="right">強(qiáng)度顯示:</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. //密碼強(qiáng)度;
          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= ["弱","中","強(qiáng)"];
          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 武志強(qiáng) 閱讀(94) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 化德县| 叙永县| 海兴县| 古丈县| 石门县| 广汉市| 集贤县| 方城县| 康乐县| 乐清市| 通江县| 南郑县| 赤城县| 寿阳县| 青田县| 荆门市| 崇信县| 庆阳市| 海口市| 易门县| 阳春市| 同心县| 广南县| 信阳市| 濮阳市| 林西县| 尼木县| 海阳市| 江达县| 乌兰察布市| 梓潼县| 宜兰县| 永顺县| 嘉义市| 密山市| 永丰县| 钦州市| 和田县| 定兴县| 峡江县| 芜湖市|