关于js处理的一些公用代码收集.(代码+demo)

关于js字符串的.prototype 的一个扩展.
//  JavaScript Document
 String.prototype.Trim = function () {return   this.replace(/(^s*)|(s*$)/g,"");}    

 String.prototype.cleanStyle
= function () {
     
    
var html =this ;
    
//清除 Hd 标记
    html = html.replace( /<Hd>(.*)</Hd>/gi, '$1' ) ;
    
//html = html.replace( /<Hd>s*</Hd>/gi, '' ) ;
    
    
    
    
//去掉其中的一个样式属性。 修如.
    //remove Styles
    html = html.replace(/<(w[^>]*) style="([^"]*)"([^>]*)/gi, "<$1$3" ) ;
    
// remove Class
    html = html.replace(/<(w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3") ;
    
    html 
= html.replace(/<!--.*-->/ig, "" ) ;
    
    
//去除多余的span 标签.
    html = html.replace( /<SPANs*[^>]*>s*&nbsp;s*</SPAN>/gi, '&nbsp;' ) ;
    html 
= html.replace( /<SPANs*[^>]*></SPAN>/gi, '' ) ;
    
    html 
= html.replace( /<(U|I|STRIKE|B|STRONG)>(.*)</1>/ig, '$2' ) ;
    
    
// Remove empty tags (three times, just to be sure).
    // This also removes any empty anchor
    html = html.replace( /<([^s>]+)(s[^>]*)?>s*</1>/g, '' ) ;
    html 
= html.replace( /<([^s>]+)(s[^>]*)?>s*</1>/g, '' ) ;
    html 
= html.replace( /<([^s>]+)(s[^>]*)?>s*</1>/g, '' ) ;
    
    
return html;
}
   

// html编码.
String.prototype.htmlEncode  = function ()
{
    text 
= this;
    
    text 
= text.replace(/&/g, "&amp;") ;
    text 
= text.replace(/"/g, "&quot;") ;
    text = text.replace(/</g, 
"&lt;") ;
    text = text.replace(/>/g, 
"&gt;") ;
    text = text.replace(/'/g, 
"&#39;") ;

    return text ;
}


//html解码.
 String.prototype.htmlDecode =function()
 {

    text = this;
    
    text = text.replace(/&amp;/g, 
"&") ;
    text = text.replace(/&quot;/g, 
""") ;
    text 
= text.replace(/&lt;/g,  "<") ;
    text 
= text.replace(/&gt;/g, ">") ;
    text 
= text.replace(/&#39;/g, "'") ;
    
return text ;
}



// 字符串方法重构, 是否包含
String.prototype.Contains = function (A)
{
    
return (this.indexOf(A)>-1);
}
;

// 字符串方法重构, 是否相等,已被扩展为是否在数组中。
String.prototype.Equals = function ()
{
    
var A=arguments;
     
if (A.length==1&&A[0].pop) A=A[0];
     
for (    var i=0; i<A.length; i++)
     

          
if (this==A[i])
         
return true;
     }
;
    
return false;
}
;


// 不区分大小写相等判断.
String.prototype.IEquals = function ()
{
    
var A=this.toUpperCase();
    
var B=arguments;
    
if (B.length==1&&B[0].pop) 
      B
=B[0];
    
for (var i=0;i<B.length;    i++)
    
{
        
if  (A==B[i].toUpperCase() ) return true;
    }
;
    
return false;
}
;

// 全部替换.
String.prototype.ReplaceAll = function (A,B)
{
    
var C=this;
    
for (var i=0;i<A.length;i++)
   
{
       C
=C.replace(A[i],B[i]);
   }
;
   
return C;
}
;



// //
/******************************************************************************************************************/
// ///字符串表单正则验证处理。
/*
*Email : /^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$/,
Phone : /^(((d{2,3}))|(d{3}-))?((0d{2,3})|0d{2,3}-)?[1-9]d{6,7}(-d{1,4})?$/,
Mobile : /^(((d{2,3}))|(d{3}-))?13d{9}$/,
Url : /^http://[A-Za-z0-9]+.[A-Za-z0-9]+[/=?%-&_~`@[]':+!]*([^""])*$/,
IdCard : /^d{15}(d{2}[A-Za-z0-9])?$/,
Currency : /^d+(.d+)?$/,
Number : /^d+$/,
Zip : /^[1-9]d{5}$/,
QQ : /^[1-9]d{4,8}$/,
Integer : /^[-+]?d+$/,
Double : /^[-+]?d+(.d+)?$/,
English : /^[A-Za-z]+$/,
Chinese : /^[Α-¥]+$/,
Username : /^[a-z]w{3,}$/i,
UnSafe : /^(([A-Z]*|[a-z]*|d*|[-_~!@#$%^&*.()[]{}?//'"]*)|.{0,5})$|s/,
Name : /^[一-龥w0-9_-]{3,11}$/i
*
*/

// 是否为URL.
String.prototype.isUrl = function ()
{
    
var Url =  /^(http(s)?://)?[A-Za-z0-9]+.[A-Za-z0-9]+[/=?%-&_~`@[]':+!]*([^""])*$/;
    return Url.test(this)?true:false;
}

//是否为邮政编码.
String.prototype.isZip = function ()
{
    var Zip = /^[1-9]d{5}$/;
   return Zip.test(this)?true:false;     
}

//返回是否为英文。
String.prototype.isEnglish = function ()
{
    var English =     /^[A-Za-z]+$/;
    return English.test(this)?true:false;     
}

//返回是否为中文。
String.prototype.isChinese  = function ()
{
    var Chinese =     /^[Α-¥]+$/;
    return Chinese.test(this)?true:false;     
}

//返回是否为数字
String.prototype.isNum = function ()
{
    var Num =     /^d+$/;
    return Num.test(this)?true:false;     
}

//返回是否为整数.
String.prototype.isInt = function ()
{
    var Int =      /^[-+]?d+$/;
    return Int.test(this)?true:false;     
}

//验证是否为unsafe
String.prototype.isUnsafe = function ()
{
    var Unsafe = /^(([A-Z]*|[a-z]*|d*|[-_~!@#$%^&*.()[]{}?//'"]*)|.{0,5})$|s/;
    return Unsafe.test(this)?true:false;     
}


//验证是否是QQ.
String.prototype.isQQ = function ()
{
    var QQ =     /^[1-9]d{4,8}$/;
    return QQ.test(this)?true:false;     
}

//验证邮件.
String.prototype.isEmail=function()
{
    var Email = /^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$/;
    return Email.test(this)?true:false;
}

//是否为手机。
String.prototype.isMobile=function()
{
        var Tel = /^(((d{2,3}))|(d{3}-))?((0d{2,3})|0d{2,3}-)?[1-9]d{6,7}(-d{1,4})?$/;
        return Tel.test(this)?true:false;
};

//验证电话号码的可用性。
String.prototype.isPhone=function()
{
        var Tel = /^(((d{2,3}))|(d{3}-))?((0d{2,3})|0d{2,3}-)?[1-9]d{6,7}(-d{1,4})?$/;
        return Tel.test(this)?true:false;
};

//是否为用户。 中英文,加下划加中划。
String.prototype.isUser=function()
{
        var Name = /^[一-龥w0-9_-]{4,16}$/i;
        return Name.test(this)?true:false;
};

String.prototype.isNick=function()
{
        var Nick = /^[一-龥w0-9_-]{3,16}$/i;
        return Nick.test(this)?true:false;
};

//密码格式
String.prototype.isPassword=function()
{
        var Password = /^.{6,16}$/i;
        return Password.test(this)?true:false;
};
//答案格式
String.prototype.isAnswer=function()
{
        var Answer = /^.{6,16}$/i;
        return Answer.test(this)?true:false;
};

//是否为用户。 中英文,加下划加中划。
String.prototype.isPic=function()
{
    PicTypeString  = picType.join("|");
    Pic = /.(replacement)$/ig;
    Pic = Pic.toString().replace ( "replacement" , PicTypeString );
    
   document.getElementById( "prompt").innerHTML = Pic;
    return eval(Pic).test(this)?true:false;
   
};
 

 

相关的demo 的

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html  xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta  http-equiv ="Content-Type"  content ="text/html; charset=utf-8"   />
< title > Untitled Document </ title >
</ head >

< body >

< input  type ="button"  value ="清除样式"    onclick =" cleanStyle(  ) " />
< input  type ="button"  value ="转HTML码"     onclick =" htmlEncode(  ) " />
< input  type ="button"  value ="解HTML码"     onclick =" htmlDecode(  ) " />
< script  src ="/static/media/js/jquery.js" ></ script >
< script  src ="../string2.js" ></ script >


< script  src ="/global/config.js" ></ script >
< script >
//清除样式。
function  cleanStyle()
{
    str 
= document.getElementById("xuxing").innerHTML;
    str 
= str.cleanStyle(); //here to cleanStyle
    document.getElementById("xuxing").innerHTML = str;
}


//将html进行编码。
function htmlEncode(){
    str 
= document.getElementById("xuxing").innerHTML;
    str 
= str.htmlEncode(); //here to cleanStyle
    document.getElementById("xuxing").innerHTML = str;
}


//解码为html.
function htmlDecode(){
    str 
= document.getElementById("xuxing").innerHTML;
    str 
= str.htmlDecode(); //here to cleanStyle
    document.getElementById("xuxing").innerHTML = str;
}


//检查电话号码格式。
function  checkPhone()
{
    str 
= document.getElementById("telePhone").value;
    
if( str.isPhone() )
    
{
       alert( 
"电话格式正确。" );
    }

    
else
    
{
        alert( 
"电话格式不正确。" );
    }

}


//用户名验证。
function  checkUser()
{
    str 
= document.getElementById("username").value;
    
    
if( str.isUser() )
    
{
       alert( 
"用户名格式正确。" );
    }

    
else
    
{
        alert( 
"用户名格式不正确。" );
    }

}



//URL验证。
function  checkUrl()
{
    str 
= document.getElementById("url").value;
    
    
if( str.isUrl() )
    
{
       alert( 
"URL格式正确。" );
    }

    
else
    
{
        alert( 
"URL格式不正确。" );
    }

}

//验证是否为数字
function checkNum ()
{

    str 
= document.getElementById("number").value;
    
    
if( str.isNum() )
    
{
       alert( 
"num格式正确。" );
    }

    
else
    
{
        alert( 
"num格式不正确。" );
    }


}


//验证是否为中文
function checkChinese()
{
    str 
= document.getElementById("chinese").value;
    
    
    
if( str.isChinese() )
    
{
       alert( 
"中文格式正确。" );
    }

    
else
    
{
        alert( 
"中文格式不正确。" );
    }

}


//验证是否为英文
function checkEnglish()
{
    str 
= document.getElementById("english").value;
    
    
    
if( str.isEnglish() )
    
{
       alert( 
"英文格式正确。" );
    }

    
else
    
{
        alert( 
"英文格式不正确。" );
    }

}


//验证是否为整数.
function checkInteger()
{
    str 
= document.getElementById("integer").value;
    
    
    
if( str.isInt() )
    
{
       alert( 
"整数格式正确。" );
    }

    
else
    
{
        alert( 
"整数格式不正确。" );
    }

}


//验证是否是QQ
function checkQQ()
{
    str 
= document.getElementById("qq").value;
    
    
    
if( str.isQQ() )
    
{
       alert( 
"QQ格式正确。" );
    }

    
else
    
{
        alert( 
"QQ格式不正确。" );
    }

}


//验证邮件
function checkEmail()
{
    str 
= document.getElementById("email").value;
    
    
    
if( str.isEmail() )
    
{
       alert( 
"邮箱格式正确。" );
    }

    
else
    
{
        alert( 
"邮箱格式不正确。" );
    }

}


//验证手机。
function checkMobile()
{
    str 
= document.getElementById("mobile").value;
    
    
    
if( str.isMobile() )
    
{
       alert( 
"手机格式正确。" );
    }

    
else
    
{
        alert( 
"手机格式不正确。" );
    }

}


//验证电话号码的可用性
function checkPhone()
{
    str 
= document.getElementById("phone").value;
    
    
    
if( str.isPhone() )
    
{
       alert( 
"电话号码格式正确。" );
    }

    
else
    
{
        alert( 
"电话号码格式不正确。" );
    }

}


//验证用户。 中英文,加下划加中划。
function checkUser()
{
    str 
= document.getElementById("user").value;
    
    
    
if( str.isUser() )
    
{
       alert( 
"格式正确。" );
    }

    
else
    
{
        alert( 
"格式不正确。" );
    }

}


//验证用户。 中英文,加下划加中划。
function checkPic()
{
    str 
= document.getElementById("pic").value;
    
if( str.isPic() )
    
{
       alert( 
"格式正确。" );
    }

    
else
    
{
        alert( 
"格式不正确。" );
    }

}


//显示弹出层
function  showAlert(str)
{
        str.value.alert();
}


</ script >
< div  id =  "xuxing" >  

< h1 >

< span     class ="font-style:inherit;"  style ="background:#3399CC;"    >  徐兴  
</ span >

< span > &nbsp; </ span >
    
    
< u > 简体 </ u >
    
< b > 粗体 </ b >
    
</ h1 >
    
< h2 >

< span     class ="font-style:inherit;"  style ="background:#3399CC;"    >  徐兴  
</ span >

</ h1 >

</ div >

< div >
        电话号码验证. 
< input  type ="text"   id ="telePhone"  name ="telePhone"   />     < button   onclick ="checkPhone()" > checkPhone  </ button >< br />
        用户名验证.    
< input  type ="text"   id ="username"  name ="username"   />     < button   onclick ="checkUser()" >    checkUser     </ button >< br />
        是否为URL 
< input  type ="text"  id ="url"  name ="url"   />   < button   onclick ="checkUrl()" >    checkUrl     </ button >< br />
         是否为数字 
< input  type ="text"  id ="number"  name ="number"   />   < button   onclick ="checkNum()" >    checkNum     </ button >< br />
        是否为中文 
< input  type ="text"  id ="chinese"  name ="chinese"   />   < button   onclick ="checkChinese()" >    checkChinese     </ button >< br />
        是否为英文 
< input  type ="text"  id ="english"  name ="english"   />   < button   onclick ="checkEnglish()" >    checkEnglish     </ button >< br />
        是否为整数 
< input  type ="text"  id ="integer"  name ="integer"   />   < button   onclick ="checkInteger()" >    checkInteger     </ button >< br />
        是否为QQ 
< input  type ="text"  id ="qq"  name ="qq"   />   < button   onclick ="checkQQ()" >    checkQQ     </ button >< br />
        是否为邮箱 
< input  type ="text"  id ="email"  name ="email"   />   < button   onclick ="checkEmail()" >    checkEmail     </ button >< br />
        是否为手机 
< input  type ="text"  id ="mobile"  name ="mobile"   />   < button   onclick ="checkMobile()" >    checkMobile     </ button >< br />          
        是否为电话号码 
< input  type ="text"  id ="phone"  name ="phone"   />   < button   onclick ="checkPhone()" >    checkPhone </ button >< br />           
        是否为用户。 中英文,加下划加中划。 
< input  type ="text"  id ="user"  name ="user"   />   < button   onclick ="checkUser()" >    checkPhone </ button >< br />      
        是否为图片版式:
< input  type ="file"  id  = "pic"  onchange ="checkPic()"   />
         
</ div >

< input  type ="button"  value ="显示这"  onclick ="showAlert(this)"   />

< div  id ="prompt" >

fdsfd
</ div >



</ body >
</ html >
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值