关闭

注册密码安全度提示(1)

204人阅读 评论(0) 收藏 举报

本段JS代码是一个朋友的,现在贴出来与大家共享

COMMON。JS文件

//引用js/css文件;
function include(path,type,title){
 var s,i,t;
 if(arguments.length < 1){
  return ;
 }
 if(arguments.length == 1){
  type = "js";
 }
 switch(type.toLowerCase()){
  case "css":
    t = document.getElementsByTagName("link");
    for(i=0;i<t.length;i++){
     if(t[i].href && t[i].href.indexOf(path)!=-1){
      return;
     }
    }
    s=document.createElement("link");
    s.rel="alternate stylesheet";
    s.type="text/css";
    s.href=path;
    s.title=title;
    s.disabled=false;
    break;
  case "js":
  case "javascript":
  default:
    t = document.getElementsByTagName("script");
    for(i=0;i<t.length;i++){
     if(t[i].src && t[i].src.indexOf(path)!=-1){
      return;
     }
    }
    s=document.createElement("script");
    s.type="text/javascript";
    s.src=path;
   break;
 }
 var h=document.getElementsByTagName("head")[0];
 h.appendChild(s);
}
//字符处理;
//去左右空格;
function trim(s){
  return rtrim(ltrim(s));
}
//去左空格;
function ltrim(s){
  return s.replace( /^/s*/, "");
}
//去右空格;
function rtrim(s){
  return s.replace( //s*$/, "");
}
//验证信息;
//空字符值;
function isEmpty(s){
 s = trim(s);
 return s.length == 0;
}
//Email;
function isEmail(s){
 s = trim(s);
  var p = /^[_/.0-9a-z-]+@([0-9a-z][0-9a-z-]+/.){1,4}[a-z]{2,3}$/i;
  return p.test(s);
}
//数字;
function isNumber(s){
 return !isNaN(s);
}
//颜色值;
function isColor(s){
 s = trim(s);
 if (s.length !=7) return false;
 return s.search(//#[a-fA-F0-9]{6}/) != -1;
}
//手机号码;
function isMobile(s){
 s = trim(s);
 var p = /13/d{9}/;
 return p.test(s);
}
//身份证;
function isCard(s){
 s = trim(s);
 var p = /^/d{15}(/d{2}[xX0-9])?$/;
 return p.test(s);
}
//URL;
function isURL(s){
 s = trim(s).toLowerCase();
 var p = /^http:////[A-Za-z0-9]+/.[A-Za-z0-9]+[//=/?%/-&_~`@[/]/':+!]*([^<>/"/"])*$/;
 return p.test(s);
}
//Phone;
function isPhone(s){
 s = trim(s);
 var p = /^((/(/d{3}/))|(/d{3}/-))?(/(0/d{2,3}/)|0/d{2,3}-)?[1-9]/d{6,7}$/;
 return p.test(s);
}
//Zip;
function isZip(s){
 s = trim(s);
 var p = /^[1-9]/d{5}$/;
 return p.test(s);
}
//Double;
function isDouble(s){
 s = trim(s);
 var p = /^[-/+]?/d+(/./d+)?$/;
 return p.test(s);
}
//Integer;
function isInteger(s){
 s = trim(s);
 var p = /^[-/+]?/d+$/;
 return p.test(s);
}
//English;
function isEnglish(s){
 s = trim(s);
 var p = /^[A-Za-z]+$/;
 return p.test(s);
}
//中文;
function isChinese(s){
 s = trim(s);
 var p = /^[/u0391-/uFFE5]+$/;
 return p.test(s);
}
//双字节
function isDoubleChar(s){
 var p = /^[^/x00-/xff]+$/;
 return p.test(s);
}
//含有中文字符
function hasChineseChar(s){
 var p = /[^/x00-/xff]/;
 return p.test(s);
}
function hasAccountChar(s){
 var p = /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,15}$/;
 return p.test(s);
}
function limitLen(s,Min,Max){
 s=trim(s);
 if(s=="") return false;
 if((s.length<Min)||(s.length>Max))
  return false;
 else
  return true;
}
//功能;
//延时事件;
function setDeferEvent(type,action,time){
 if (trim(time).length == 0) time = 1;
 if (typeof(time)!="number") time = 1;
 
 switch(type.toLowerCase()){
  case "go":
   window.setTimeout("window.location='"+ action +"'",time);
   break;
  case "alert":
   window.setTimeout("alert('"+ action +"')",time);
   break;
  case "js":
  case "javascript":
   window.setTimeout("'"+ action.toString() +"'",time);
   break;
  default:
   alert("Nothing will do!");
   break
 } 
}
function addLoadListener(handler){
 if (typeof window.addEventListener != 'undefined')
  window.addEventListener('load', handler, false);
 else if (typeof document.addEventListener != 'undefined')
  document.addEventListener('load', handler, false);
 else if (typeof window.attachEvent != 'undefined')
  window.attachEvent('onload', handler);
};
function addEventListener(element, eventType, handler, capture)
{
 try
 {
  if (element.addEventListener)
   element.addEventListener(eventType, handler, capture);
  else if (element.attachEvent)
   element.attachEvent("on" + eventType, handler);
 }
 catch (e) {}
};

function removeEventListener(element, eventType, handler, capture)
{
 try
 {
  if (element.removeEventListener)
   element.removeEventListener(eventType, handler, capture);
  else if (element.detachEvent)
   element.detachEvent("on" + eventType, handler);
 }
 catch (e) {}
};
//Image;
function preloadImages(){
 var d = document;
 if(d.images){
  if(!d.p_i_a) d.p_i_a = new Array();
  var i,j=d.p_i_a.length,a=arguments;
  for(i=0;i<a.length;i++){
   d.p_i_a[j]= new Image();
   d.p_i_a[j++].src = a[i];
  }
 }
}
//Ajax功能;
function loadAjaxElement(e,u,p,f,l){
 if(arguments.length < 3){
  return ;
 }
 o = $(e);
 o.innerHTML = l;
 p = $H(p).toQueryString();
 new Ajax.Updater(     
                    {success: e},
                    u,
                    {method: 'get', parameters: p, onFailure: f});
}
function loadAjaxData(u,p,s,f){
 if(arguments.length < 3){
  return ;
 }
 p = $H(p).toQueryString();
 new Ajax.Request(     
                    u,
                    {method: 'get', parameters: p, onSuccess:s,onFailure: f});
}
function sendAjaxElement(e,u,p,f,l){
 if(arguments.length < 3){
  return ;
 }
 o = $(e);
 o.innerHTML = l;
 p = $H(p).toQueryString();
 new Ajax.Updater(     
                    {success: e},
                    u,
                    {method: 'post', parameters: p, onFailure: f});
}
function sendAjaxData(u,p,s,f){
 if(arguments.length < 3){
  return ;
 }
 p = $H(p).toQueryString();
 new Ajax.Request(     
                    u,
                    {method: 'post', parameters: p, onSuccess:s,onFailure: f});
}

PASSWORDSTRENGTH。JS文件

//密码强度;
function PasswordStrength(showed){
 this.showed = (typeof(showed) == "boolean")?showed:true;
 this.styles = new Array();
 this.styles[0] = {backgroundColor:"#EBEBEB",borderLeft:"solid 1px #FFFFFF",borderRight:"solid 1px #BEBEBE",borderBottom:"solid 1px #BEBEBE"};
 this.styles[1] = {backgroundColor:"#FF4545",borderLeft:"solid 1px #FFFFFF",borderRight:"solid 1px #BB2B2B",borderBottom:"solid 1px #BB2B2B"};
 this.styles[2] = {backgroundColor:"#FFD35E",borderLeft:"solid 1px #FFFFFF",borderRight:"solid 1px #E9AE10",borderBottom:"solid 1px #E9AE10"};
 this.styles[3] = {backgroundColor:"#95EB81",borderLeft:"solid 1px #FFFFFF",borderRight:"solid 1px #3BBC1B",borderBottom:"solid 1px #3BBC1B"};
 
 this.labels= ["弱","中","强"];

 this.divName = "pwd_div_"+Math.ceil(Math.random()*100000);
 this.minLen = 5;
 
 this.width = "150px";
 this.height = "16px";
 
 this.content = "";
 
 this.selectedIndex = 0;
 
 this.init();
}
PasswordStrength.prototype.init = function(){
 var s = '<table cellpadding="0" id="'+this.divName+'_table" cellspacing="0" style="width:'+this.width+';height:'+this.height+';">';
 s += '<tr>';
 for(var i=0;i<3;i++){
  s += '<td id="'+this.divName+'_td_'+i+'" width="33%" align="center"><span style="font-size:1px">&nbsp;</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>';
 }
 s += '</tr>';
 s += '</table>';
 this.content = s;
 if(this.showed){
  document.write(s);
  this.copyToStyle(this.selectedIndex);
 }
}
PasswordStrength.prototype.copyToObject = function(o1,o2){
 for(var i in o1){
  o2[i] = o1[i];
 }
}
PasswordStrength.prototype.copyToStyle = function(id){
 this.selectedIndex = id;
 for(var i=0;i<3;i++){
  if(i == id-1){
   this.$(this.divName+"_label_"+i).style.display = "inline";
  }else{
   this.$(this.divName+"_label_"+i).style.display = "none";
  }
 }
 for(var i=0;i<id;i++){
  this.copyToObject(this.styles[id],this.$(this.divName+"_td_"+i).style);  
 }
 for(;i<3;i++){
  this.copyToObject(this.styles[0],this.$(this.divName+"_td_"+i).style);
 }
}
PasswordStrength.prototype.$ = function(s){
 return document.getElementById(s);
}
PasswordStrength.prototype.setSize = function(w,h){
 this.width = w;
 this.height = h;
}
PasswordStrength.prototype.setMinLength = function(n){
 if(isNaN(n)){
  return ;
 }
 n = Number(n);
 if(n>1){
  this.minLength = n;
 }
}
PasswordStrength.prototype.setStyles = function(){
 if(arguments.length == 0){
  return ;
 }
 for(var i=0;i<arguments.length && i < 4;i++){
  this.styles[i] = arguments[i];
 }
 this.copyToStyle(this.selectedIndex);
}
PasswordStrength.prototype.write = function(s){
 if(this.showed){
  return ;
 }
 var n = (s == 'string') ? this.$(s) : s;
 if(typeof(n) != "object"){
  return ;
 }
 n.innerHTML = this.content;
 this.copyToStyle(this.selectedIndex);
}
PasswordStrength.prototype.update = function(s){
 if(s.length < this.minLen){
  this.copyToStyle(0);
  return;
 }
 var ls = -1;
 if (s.match(/[a-z]/ig)){
  ls++;
 }
 if (s.match(/[0-9]/ig)){
  ls++;
 }
  if (s.match(/(.[^a-z0-9])/ig)){
  ls++;
 }
 if (s.length < 6 && ls > 0){
  ls--;
 }
  switch(ls) {
   case 0:
    this.copyToStyle(1);
    break;
   case 1:
    this.copyToStyle(2);
    break;
   case 2:
    this.copyToStyle(3);
    break;
   default:
    this.copyToStyle(0);
  }
}

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/honkerhero/archive/2007/04/30/1592591.aspx

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:22004次
    • 积分:376
    • 等级:
    • 排名:千里之外
    • 原创:11篇
    • 转载:31篇
    • 译文:0篇
    • 评论:0条