天天看點

一些常用的腳本驗證集合

//----------

//珍惜别人的勞動成果,整理自20101006

//----------

String.prototype.ltrim = function() 

return this.replace(/(^/s*)/g, ""); 

String.prototype.Mid = function(start,len) 

 if(isNaN(start)&&start<0) 

 { 

 return ""; 

 } 

 if(isNaN(len)&&len<0) 

 { 

 return ""; 

 } 

 return this.substring(start,len); 

String.prototype.rtrim = function() 

return this.replace(/(/s*$)/g, ""); 

String.prototype.trim = function() 

return this.replace(/(^/s*)|(/s*$)/g, ""); 

String.prototype.InStr = function(str) 

if(str==null) 

str = ""; 

return this.indexOf(str); 

String.prototype.InStrRev = function(str) 

if(str==null) 

str = ""; 

return this.lastIndexOf(str); 

}

String.prototype.LengthW = function() 

return this.replace(/[^/x00-/xff]/g,"**").length; 

String.prototype.isIP = function() 

var reSpaceCheck = /^(/d+)/.(/d+)/.(/d+)/.(/d+)$/;

if (reSpaceCheck.test(this)) 

this.match(reSpaceCheck); 

if (RegExp.$1 <= 255 && RegExp.$1 >= 0 

 && RegExp.$2 <= 255 && RegExp.$2 >= 0 

 && RegExp.$3 <= 255 && RegExp.$3 >= 0 

 && RegExp.$4 <= 255 && RegExp.$4 >= 0) 

return true;     

else 

return false; 

else 

return false; 

String.prototype.isDate = function() 

var r = str.match(/^(/d{1,4})(-|//)(/d{1,2})/2(/d{1,2}) (/d{1,2}):(/d{1,2}):(/d{1,2})$/); 

if(r==null) 

return false; 

var d = new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]); 

return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]&&d.getSeconds()==r[7]); 

}

String.prototype.isMobile = function() 

return /^0{0,1}13[0-9]{9}$/.test(this); 

String.prototype.isEmail = function() 

return /^/w+((-/w+)|(/./w+))*/@[A-Za-z0-9]+((/.|-)[A-Za-z0-9]+)*/.[A-Za-z0-9]+$/.test(this); 

String.prototype.isZipCode = function() 

return /^[//d]{6}$/.test(this); 

String.prototype.existChinese = function() 

//[/u4E00-/u9FA5]為漢字﹐[/uFE30-/uFFA0]為全角符號 

return /^[/x00-/xff]*$/.test(this); 

String.prototype.isFileName = function() 

return !/[ 

//String.prototype.isUrl = function() 

//{ 

//    return /^http:([/w-]+/.)+[/w-]+(/[/w-./?%&=]*)?$/.test(this); 

//} 

String.prototype.isIDCard = function() 

var iSum=0; 

var info=""; 

var sId = this; 

var aCity={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"遼甯",22:"吉林",23:"黑龍江",31:"上海",32:"江蘇",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山東",41:"河南",42:"湖北",43:"湖南",44:"廣東",45:"廣西",46:"海南",50:"重慶",51:"四川",52:"貴州",53:"雲南",54:"西藏",61:"陝西",62:"甘肅",63:"青海",64:"甯夏",65:"新疆",71:"台灣",81:"香港",82:"澳門",91:"國外"};

if(!/^/d{17}(/d|x)$/i.test(sId)) 

return false; 

sId=sId.replace(/x$/i,"a"); 

//非法地區 

if(aCity[parseInt(sId.substr(0,2))]==null) 

return false; 

var sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2));

var d=new Date(sBirthday.replace(/-/g,"/"))

//非法生日 

if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate())) 

return false; 

for(var i = 17;i>=0;i--) 

iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11); 

if(iSum%11!=1) 

return false; 

return true; 

}

String.prototype.isPhoneCall = function() 

return /(^[0-9]{3,4}/-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^/([0-9]{3,4}/)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/.test(this); 

String.prototype.isNumeric = function(flag) 

//驗證是否是數字 

if(isNaN(this)) 

return false; 

switch(flag) 

case null://數字 

case "": 

return true; 

case "+"://正數 

return /(^/+?|^/d?)/d*/.?/d+$/.test(this); 

case "-"://負數 

return/^-/d*/.?/d+$/.test(this); 

case "i"://整數 

return/(^-?|^/+?|/d)/d+$/.test(this); 

case "+i"://正整數 

return/(^/d+$)|(^/+?/d+$)/.test(this); 

case "-i"://負整數 

return/^[-]/d+$/.test(this); 

case "f"://浮點數 

return/(^-?|^/+?|^/d?)/d*/./d+$/.test(this); 

case "+f"://正浮點數 

return/(^/+?|^/d?)/d*/./d+$/.test(this); 

case "-f"://負浮點數 

return/^[-]/d*/./d$/.test(this); 

default://預設 

return true; 

String.prototype.toCase = function() 

var tmp = ""; 

for(var i=0;i<this.length;i++) 

if(this.charCodeAt(i)>0&&this.charCodeAt(i)<255) 

tmp += String.fromCharCode(this.charCodeAt(i)+65248); 

else 

tmp += String.fromCharCode(this.charCodeAt(i)); 

return tmp 

String.prototype.toHtmlEncode = function()

var str = this; 

str=str.replace("&","&amp;"); 

str=str.replace("<","&lt;"); 

str=str.replace(">","&gt;"); 

str=str.replace("’","&apos;"); 

str=str.replace("/"","&quot;"); 

return str; 

//’********************************************************* 

// ’ Purpose: 判斷輸入是否為整數字 

// ’ Inputs:   String 

// ’ Returns:  True, False 

//’********************************************************* 

function onlynumber(str) 

    var i,strlength,tempchar; 

    str=CStr(str); 

   if(str=="") return false; 

    strlength=str.length; 

    for(i=0;i<strlength;i++) 

    { 

        tempchar=str.substring(i,i+1); 

        if(!(tempchar==0||tempchar==1||tempchar==2||tempchar==3||tempchar==4||tempchar==5||tempchar==6||tempchar==7||tempchar==8||tempchar==9)) 

        { 

        alert("隻能輸入數字 "); 

        return false; 

        } 

    } 

    return true; 

//’********************************************************* 

//’********************************************************* 

// ’ Purpose: 判斷輸入是否為數值(包括小數點) 

// ’ Inputs:   String 

// ’ Returns:  True, False 

//’********************************************************* 

function IsFloat(str) 

{ var tmp; 

   var temp; 

   var i; 

   tmp =str; 

if(str=="") return false;  

for(i=0;i<tmp.length;i++) 

{temp=tmp.substring(i,i+1); 

if((temp>='0' && temp<= '9')||(temp == '.')){} //check input in 0-9 and ’.’ 

else   { return false;} 

return true; 

//’********************************************************* 

// ’ Purpose: 判斷輸入是否為電話号碼 

// ’ Inputs:   String 

// ’ Returns:  True, False 

//’********************************************************* 

function isphonenumber(str) 

   var i,strlengh,tempchar; 

   str=CStr(str); 

   if(str=="") return false; 

   strlength=str.length; 

   for(i=0;i<strlength;i++) 

   { 

        tempchar=str.substring(i,i+1); 

        if(!(tempchar==0||tempchar==1||tempchar==2||tempchar==3||tempchar==4||tempchar==5||tempchar==6||tempchar==7||tempchar==8||tempchar==9||tempchar== '-')) 

        { 

        alert("電話号碼隻能輸入數字和中劃線 "); 

        return(false); 

        }    

   } 

   return(true); 

function isNotYinhao(s)

{  

    var yin;

 var temp="'";

 for(yin=0; yin < s.length; yin++ )

 {

  var ch = s.charAt(yin);

  if(temp.indexOf(ch)>=0)

  {

   return true;

  }

 }

 return false;

}

function isNumber(text,name)

{

  var temp="0123456789";

   for(j=0; j<text.value.length; j++ )

   {   

     var ch = text.value.Trim().charAt(j);

  if(temp.indexOf(ch)==-1)

  {

   alert(name+"應為數字類型!");

   text.focus();

   return true;

  } 

   }

}

function isChar(text,addtemp,name,include)

{

  var temp="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"+addtemp;

   for(j=0; j<text.value.length; j++ )

   {   

     var ch = text.value.Trim().charAt(j);

  if(temp.indexOf(ch)==-1)

  {

   alert(name+"中不允許包含'"+include+"'等字元!");

   text.focus();

   break;

  } 

   }

}

function isNull(text,name)

{

 if(text.value.Trim()==null||text.value.Trim()=="")

 {

  alert(name+"不能為空!");

  text.focus();

  return true;

 }

}

繼續閱讀