/*$#################################################$*/ //驗證字符串數據 function CheckData(valname,val,valimode,limitlen){ if( val.value.indexOf("<",0)>0 || val.value.indexOf(">",0)>0){ alert(valname+"不能包含<或>字符!。"); val.focus(); val.select(); return false; } if(valimode==1){ if(isblank(val.value)){ alert(valname+"不能爲空!"); val.focus(); val.select(); return false; } tmp=new String(val.value) if(istoolong(tmp,limitlen)){ alert(valname+"不能超過"+limitlen+"個字符!"); val.focus(); val.select(); return false; } } else{ tmp=new String(val.value) if(istoolong(tmp,limitlen)){ alert(valname+"不能超過"+limitlen+"個字符!!"); val.focus(); val.select(); return false; } } return true; } function isblank(str){ var i; var len=str.length; for(i=0;i<len;++i){ if(str.charAt(i)!=" ") return false; } return true; } function istoolong(str,i){ var len=str.length; if(i==0) return false; else{ if(len>i) return true; else return false; } } //驗證用戶名 function CheckUserName(val,min,max){ var len = val.value.length; if (len<min || len>max){ alert("用戶名長度不正確,應爲"+min+"-"+max+"個英文字母、數字。"); val.focus(); val.select(); return false; } for (i=0;i<val.value.length;i++){ var ch = val.value.charAt(i); if ( (ch<"0" || ch>"9") && (ch<"a" || ch >"z")){ alert("用戶名必須由小寫字母或數字組成。"); val.focus(); val.select(); return false; } } var first =val.value.charAt(0) if (first<"a" || ch >"z"){ alert("用戶名必須由小寫字母開頭。"); val.focus(); val.select(); return false; } return true; } //驗證密碼 function CheckPassWord(val,min,max){ var len = val.value.length; if (len<min || len>max){ alert("密碼長度不正確,應爲"+min+"-"+max+"個英文字母、數字。"); val.focus(); val.select(); return false; } for (i=0;i<val.value.length;i++){ var ch = val.value.charAt(i); if ( (ch<"0" || ch>"9") && (ch<"a" || ch >"z")){ alert("密碼必須由小寫字母或數字組成。"); val.focus(); val.select(); return false; } } return true; } //驗證密碼匹配 function IsSame(val1,val2){ if (val1.value != val2.value ){ alert("兩次輸入的密碼不相同。"); val2.focus(); val2.select(); return false; } else return true; } //驗證Email function CheckEmail(val,mode){ if((mode==0) && (val.value=="")){ return true; } var etext var elen var i var aa etext=val.value elen=etext.length if (elen<5){ alert("請輸入正確的Email。"); val.focus(); val.select(); return false; } i= etext.indexOf("@",0) if (i==0 || i==-1 || i==elen-1){ alert("請輸入正確的Email。"); val.focus(); val.select(); return false; } else{ if (etext.indexOf("@",i+1)!=-1){ alert("請輸入正確的Email。"); val.focus(); val.select(); return false; } } if (etext.indexOf("..",i+1)!=-1){ alert("請輸入正確的Email。"); val.focus(); val.select(); return false; } i=etext.indexOf(".",0) if (i==0 || i==-1 || etext.charAt(elen-1)=='.'){ alert("請輸入正確的Email。"); val.focus(); val.select(); return false; } if ( etext.charAt(0)=='-' || etext.charAt(elen-1)=='-'){ alert("請輸入正確的Email。"); val.focus(); val.select(); return false; } if ( etext.charAt(0)=='_' || etext.charAt(elen-1)=='_'){ alert("請輸入正確的Email。"); val.focus(); val.select(); return false; } for (i=0;i<=elen-1;i++){ aa=etext.charAt(i) if (!((aa=='.') || (aa=='@') || (aa=='-') ||(aa=='_') || (aa>='0' && aa<='9') || (aa>='a' && aa<='z') || (aa>='A' && aa<='Z'))){ alert("請輸入正確的Email。"); val.focus(); val.select(); return false; } } return true; } //驗證用戶身高 function CheckValHeight(val,min,max,mode){ if((mode==0) && (val.value=="")){ return true; } var len = val.value.length; if (len<min || len>max){ alert("請輸入正確的身高數據。"); val.focus(); val.select(); return false; } var val2 = val.value; for (i=0;i<val2.length;i++){ var ch = val2.charAt(i); if ( (ch<"0" || ch>"9") ){ alert("身高數據必須由有效數字組成。"); val.focus(); val.select(); return false; } } if (isNaN(val2)){ alert("身高數據必須由有效數字組成。"); val.focus(); val.select(); return false; } else if (parseInt(val2)<=0){ alert("身高數據必須由有效數字組成。"); val.focus(); val.select(); return false; } else if (val2.indexOf('0')==0){ alert("身高數據必須由有效數字組成。"); val.focus(); val.select(); return false; } return true; } //驗證用戶體重 function CheckValWeight(val,min,max,mode){ if((mode==0) && (val.value=="")){ return true; } var len = val.value.length; if (len<min || len>max){ alert("請輸入正確的體重數據。"); val.focus(); val.select(); return false; } var val2 = val.value; for (i=0;i<val2.length;i++){ var ch = val2.charAt(i); if ( (ch<"0" || ch>"9") ){ alert("體重數據必須由有效數字組成。"); val.focus(); val.select(); return false; } } if (isNaN(val2)){ alert("體重數據必須由有效數字組成。"); val.focus(); val.select(); return false; } else if (parseInt(val2)<=0){ alert("體重數據必須由有效數字組成。"); val.focus(); val.select(); return false; } else if (val2.indexOf('0')==0){ alert("體重數據必須由有效數字組成。"); val.focus(); val.select(); return false; } return true; } //驗證數值 function CheckNumber(val,min,max,mode){ if((mode==0) && (val.value=="")){ return true; } var len = val.value.length; if (len<min || len>max){ alert("請輸入正確的數值。"); val.focus(); val.select(); return false; } var val2 = val.value; for (i=1;i<val2.length;i++){ var ch = val2.charAt(i); if ( (ch<"0" || ch>"9" ) ){ alert("請輸入正確的數值。"); val.focus(); val.select(); return false; } } if (isNaN(val2)){ alert("請輸入正確的數值。"); val.focus(); val.select(); return false; } else if (val2.indexOf('0')==0 && len>1){ alert("請輸入正確的有效數值。"); val.focus(); val.select(); return false; } else if (val2.indexOf('-')==0 && val2.indexOf('0')==1){ alert("請輸入正確的有效數值。"); val.focus(); val.select(); return false; } return true; } //驗證正數值 function CheckPositive(val,min,max,mode){ if((mode==0) && (val.value=="")){ return true; } if(!CheckNumber(val,min,max,mode)){ val.focus(); val.select(); return false; } if (parseInt(val.value)<=0){ alert("數據必須爲有效的正整數。"); val.focus(); val.select(); return false; } return true; } //驗證負數值 function CheckNegative(val,min,max,mode){ if((mode==0) && (val.value=="")){ return true; } if(!CheckNumber(val,min,max,mode)){ val.focus(); val.select(); return false; } if (parseInt(val.value)>=0){ alert("數據必須爲有效的負整數。"); val.focus(); val.select(); return false; } return true; } //驗證浮點數值 function CheckFloat(val,min,max,mode){ if((mode==0) && (val.value=="")){ return true; } var len = val.value.length; if (len<min || len>max){ alert("請輸入正確的浮點數值。"); val.focus(); val.select(); return false; } var val2 = val.value; for (i=1;i<val2.length;i++){ var ch = val2.charAt(i); if ( (ch<"0" || ch>"9" ) ){ if(ch!="."){ alert("請輸入正確的浮點數值。"); val.focus(); val.select(); return false; } } } if (isNaN(val2)){ alert("請輸入正確的浮點數值。"); val.focus(); val.select(); return false; } else if (val2.indexOf('0')==0 && val2.indexOf('.')!=1){ alert("請輸入正確的有效浮點數值。"); val.focus(); val.select(); return false; } else if (val2.indexOf('-')==0 && val2.indexOf('0')==1 && val2.indexOf('.')!=2){ alert("請輸入正確的有效浮點數值。"); val.focus(); val.select(); return false; } else if (val2.indexOf('-')==0 && val2.indexOf('.')==1){ alert("請輸入正確的有效浮點數值。"); val.focus(); val.select(); return false; } else if (val2.indexOf('.')==0){ alert("請輸入正確的有效浮點數值。"); val.focus(); val.select(); return false; } return true; } //驗證文件路徑 function CheckPath(val,mode){ if((mode==0) && (val.value=="")){ return true; } var val2 = val.value; if ( val2.indexOf(':\\')>0 ){ var isNot=" !@#$^*()'`~|]}[{;.>,<?%&+="; if (val2.indexOf('\"')>0){ alert("請輸入正確的文件路徑。"); val.focus(); val.select(); return false; } else{ for (var i = 0;i < val2.length;i++){ for (var x = 1 ;x < isNot.length;x++){ if (val2.charAt(i)==isNot.charAt(x)){ alert("請輸入正確的文件路徑。"); val.focus(); val.select(); return false; } } } } } else{ alert("請輸入正確的文件路徑。"); val.focus(); val.select(); return false; } return true; } //驗證URL function CheckURL(val,mode){ if((mode==0) && (val.value=="")){ return true; } var val2 = val.value; if ( val2.indexOf('://')>0 ){ var isNot=" !@$^*()'`~|]}[{;.>,<"; if (val2.indexOf('\"')>0){ alert("請輸入正確的URL。"); val.focus(); val.select(); return false; } else{ for (var i = 0;i < val2.length;i++){ for (var x = 1 ;x < isNot.length;x++){ if (val2.charAt(i)==isNot.charAt(x)){ alert("請輸入正確的URL。"); val.focus(); val.select(); return false; } } } } } else{ alert("請輸入正確的URL。"); val.focus(); val.select(); return false; } return true; } //驗證身份證 function CheckChinaIDCard_J(val,mode){ if((mode==0) && (val.value=="")){ return true; } sNo = val.value.toString() if (!isInteger(sNo)) { alert("請輸入正確的身份證。"); val.focus(); val.select(); return false } switch(sNo.length){ case 15: if (isValidDate(sNo.substr(6,2),sNo.substr(8,2),sNo.substr(10,2))) { return true } case 17: if (isValidDate(sNo.substr(6,4),sNo.substr(10,2),sNo.substr(12,2))) { return true } } alert("請輸入正確的身份證。"); val.focus(); val.select(); return false } function isInteger(sNum) { var num num=new RegExp('[^0-9_]','') if (isNaN(sNum)) { return false } else { if (sNum.search(num)>=0) { return false } else { return true } } } //驗證Zip function CheckZip(val,slen,mode){ if((mode==0) && (val.value=="")){ return true; } if(val.value.length!=slen){ alert('請輸入正確的郵政編碼!!'); val.focus(); val.select(); return false } var r1 r1 = new RegExp('[^0-9]',''); if ( val.value.search(r1) >= 0 ) { alert('請輸入正確的郵政編碼!!'); val.focus(); val.select(); return false } else return true; } 電話號碼的驗證 要求: (1)電話號碼由數字、"("、")"和"-"構成 (2)電話號碼爲3到8位 (3)若是電話號碼中包含有區號,那麼區號爲三位或四位 (4)區號用"("、")"或"-"和其餘部分隔開 (5)移動電話號碼爲11或12位,若是爲12位,那麼第一位爲0 (6)11位移動電話號碼的第一位和第二位爲"13" (7)12位移動電話號碼的第二位和第三位爲"13" 根據這幾條規則,能夠與出如下正則表達式: (^[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}$) <script language="javascript"> function PhoneCheck(s) { var str=s; var reg=/(^[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}$)/ alert(reg.test(str)); } </script> <input type=text name="iphone"> <input type=button onclick="PhoneCheck(document.all.iphone.value)" value="Check">
//****************************************************************
//* 名 稱:DataLength
//* 功 能:計算數據的長度
//* 入口參數:fData:須要計算的數據
//* 出口參數:返回fData的長度(Unicode長度爲2,非Unicode長度爲1)
//*****************************************************************
function DataLength(fData)
{
var intLength=0
for (var i=0;i<fData.length;i++)
{
if ((fData.charCodeAt(i) < 0) || (fData.charCodeAt(i) > 255))
intLength=intLength+2
else
intLength=intLength+1
}
return intLength
}javascript
//****************************************************************
//* 名 稱:DataLength
//* 功 能:計算數據的長度
//* 入口參數:fData:須要計算的數據
//* 出口參數:返回fData的長度(Unicode長度爲2,非Unicode長度爲1)
//*****************************************************************
function DataLength(fData)
{
var intLength=0
for (var i=0;i<fData.length;i++)
{
if ((fData.charCodeAt(i) < 0) || (fData.charCodeAt(i) > 255))
intLength=intLength+2
else
intLength=intLength+1
}
return intLength
}html
//****************************************************************
//* 名 稱:IsEmpty
//* 功 能:判斷是否爲空
//* 入口參數:fData:要檢查的數據
//* 出口參數:True:空
//* False:非空
//*****************************************************************
function IsEmpty(fData)
{
return ((fData==null) || (fData.length==0) )
}java
//****************************************************************
//* 名 稱:IsDigit
//* 功 能:判斷是否爲數字
//* 入口參數:fData:要檢查的數據
//* 出口參數:True:是0到9的數字
//* False:不是0到9的數字
//*****************************************************************
function IsDigit(fData)
{
return ((fData>="0") && (fData<="9"))
}git
//****************************************************************
//* 名 稱:IsInteger
//* 功 能:判斷是否爲正整數
//* 入口參數:fData:要檢查的數據
//* 出口參數:True:是整數,或者數據是空的
//* False:不是整數
//*****************************************************************
function IsInteger(fData)
{
//若是爲空,返回true
if (IsEmpty(fData))
return true
if ((isNaN(fData)) || (fData.indexOf(".")!=-1) || (fData.indexOf("-")!=-1))
return false
return true
}正則表達式
//****************************************************************
//* 名 稱:IsEmail
//* 功 能:判斷是否爲正確的Email地址
//* 入口參數:fData:要檢查的數據
//* 出口參數:True:正確的Email地址,或者空
//* False:錯誤的Email地址
//*****************************************************************
function IsEmail(fData)
{
if (IsEmpty(fData))
return true
if (fData.indexOf("@")==-1)
return false
var NameList=fData.split("@");
if (NameList.length!=2)
return false
if (NameList[0].length<1 )
return false
if (NameList[1].indexOf(".")<=0)
return false
if (fData.indexOf("@")>fData.indexOf("."))
return false
if (fData.indexOf(".")==fData.length-1)
return false
return true
}iphone
//****************************************************************
//* 名 稱:IsPhone
//* 功 能:判斷是否爲正確的電話號碼(能夠含"()"、"()"、"+"、"-"和空格)
//* 入口參數:fData:要檢查的數據
//* 出口參數:True:正確的電話號碼,或者空
//* False:錯誤的電話號碼
//* 錯誤信息:
//*****************************************************************
function IsPhone(fData)
{
var str;
var fDatastr="";
if (IsEmpty(fData))
return true
for (var i=0;i<fData.length;i++)
{
str=fData.substring(i,i+1);
if (str!="(" && str!=")" && str!="(" && str!=")" && str!="+" && str!="-" && str!=" ")
fDatastr=fDatastr+str;
}
//alert(fDatastr);
if (isNaN(fDatastr))
return false
return true
}函數
//****************************************************************
//* 名 稱:IsPlusNumeric
//* 功 能:判斷是否爲正確的正數(能夠含小數部分)
//* 入口參數:fData:要檢查的數據
//* 出口參數:True:正確的正數,或者空
//* False:錯誤的正數
//* 錯誤信息:
//*****************************************************************
function IsPlusNumeric(fData)
{
if (IsEmpty(fData))
return true
if ((isNaN(fData)) || (fData.indexOf("-")!=-1))
return false
return true
}編碼
//****************************************************************
//* 名 稱:IsNumeric
//* 功 能:判斷是否爲正確的數字(能夠爲負數,小數)
//* 入口參數:fData:要檢查的數據
//* 出口參數:True:正確的數字,或者空
//* False:錯誤的數字
//* 錯誤信息:
//*****************************************************************
function IsNumeric(fData)
{
if (IsEmpty(fData))
return true
if (isNaN(fData))
return false
return true
}spa
//****************************************************************
//* 名 稱:IsIntegerInRange
//* 功 能:判斷一個數字是否在指定的範圍內
//* 入口參數:fInput:要檢查的數據
//* fLower:檢查的範圍下限,若是沒有下限,請用null
//* fHigh:檢查的上限,若是沒有上限,請用null
//* 出口參數:True:在指定的範圍內
//* False:超出指定範圍
//*****************************************************************
function IsIntegerInRange(fInput,fLower,fHigh)
{
if (fLower==null)
return (fInput<=fHigh)
else if (fHigh==null)
return (fInput>=fLower)
else
return ((fInput>=fLower) && (fInput<=fHigh))
}code