验证脚本

/
//基本函數

//判斷是否是數位
function isNumeric(strNumber)
{
 if(isEmpty(strNumber)) return false;
 return (strNumber.search(/^(-|/+)?/d+(/./d+)?$/) != -1);
}
//判斷是否是大於0的數位
function isUNumeric(strNumber)
{
 if(isEmpty(strNumber)) return false;
 return (strNumber.search(/^/d+(/./d+)?$/) != -1);
}
//判斷是否是整數
function isInt(strInteger)
{
 if(isEmpty(strInteger)) return false;
 return (strInteger.search(/^(-|/+)?/d+$/) != -1);
}
//判斷是否是大於0的整數
function isUInt(strInteger)
{
 if(isEmpty(strInteger)) return false;
 return (strInteger.search(/^/d+$/) != -1);
}

//判斷是否是日期
function isDate(pi_year,pi_month,pi_day)
{
 var ld_date;
 
 if(pi_month=="" || pi_month==undefined) //pi_year=日期
 {
  pi_year=String(pi_year);
  pi_year=pi_year.replace( /-/g,"/");
  ld_date=new Date(pi_year);
  if(isNaN(ld_date)) return false;
  var li_len=pi_year.length;
  var li_temp=pi_year.substring(li_len-2,li_len);
  if(isNaN(parseInt(li_temp))) li_temp=pi_year.substring(li_len-1,li_len);
  li_temp=parseInt(li_temp);
  if(ld_date.getDate()==li_temp) return true;
  else return false;
 }
 else //pi_year=年,pi_month=月,pi_day=日
  ld_date=new Date(pi_year,pi_month-1,pi_day); 
 if(ld_date.getDate()==pi_day) return true;
 return false;
}

//判斷是否為空
function isEmpty(strVal,defVal)
{
 if(strVal==undefined) return true;
 
 var reg=//s*$/;   //消除後面的空格
 strVal=strVal.replace(reg,"");
 
 if(strVal.length==0) return true;
 if(defVal==undefined || defVal==null || defVal.length==0) return false;
 if(defVal==strVal) return true;
 return false;
}

//消除字元前後的空白
function trim(strVal)
{
 if(strVal==undefined) return strVal;
 
 strVal=String(strVal);
 var reg=/^/s*/;  //消除前面的空格
 strVal=strVal.replace(reg,"");
 reg=//s*$/;   //消除後面的空格
 strVal=strVal.replace(reg,"");
 return strVal;
}

//判斷email格式
function isEmail(strEmail)
{
 if(isEmpty(strEmail)) return false;
 var reg=/^/w+((-/w+)|(/./w+))*/@[A-Za-z0-9]+((/.|-)[A-Za-z0-9]+)*/.[A-Za-z0-9]+$/;
 return (strEmail.search(reg) != -1);
}

//判斷是否符合reg指定的格式
function isReg(strVal,reg)
{
 if(isEmpty(strVal)) return false;
 strVal=trim(strVal);
 //var reg=/^/w+((-/w+)|(/./w+))*/@[A-Za-z0-9]+((/.|-)[A-Za-z0-9]+)*/.[A-Za-z0-9]+$/;
 return (strVal.search(reg) != -1);
}

//判斷字符長度是否在指定范圍內
function isLength(strVal,ValRang)
{
 strVal=trim(strVal);
 return isRange(strVal.length,ValRang);
}

//判斷數字范圍
function isRange(objVal,ValRang)
{
 if(!isNumeric(objVal)) return false;
 if(!isUInt(ValRang)) return false;
 
 if(ValRang==objVal || ValRang.length==0)
  return true;
 
 var arValRang=ValRang.split(",");
 
 if(arValRang.length<2)
 {
  if(objVal==ValRang) return true;
  return false;
 }
 
 var nVal=parseFloat(objVal);
 if(arValRang[0]=="*")
 {
  if(nVal<=parseFloat(arValRang[1])) return true;
  //strText+="最大為"+arValRang[1];
 }
 else if(arValRang[1]=="*")
 {
  if(nVal>=parseFloat(arValRang[0])) return true;
  //strText+="最小為"+arValRang[0];
 }
 else
 {
  if(nVal>=parseFloat(arValRang[0]) && nVal<=parseFloat(arValRang[1])) return true;
  //strText+=arValRang[0]+"--"+arValRang[1];
 }
 //strText+=")";
 //alert(strText);
 return false;
}
///
/*帶form,提示的判斷函數
thisObj=需要判斷的元素名,即<input>的name值
bEmpty=true:允許為空(預設=true),即為空時不判斷
ValRang=取值範圍。空('')-不判斷範圍,(20,100)-表示範圍在前20-100之間
(*,100)-表示最大是100,(20,*)-表示最小是20
strText=顯示的控制項的中文名稱(用於顯示提示)
*/
/
//判斷是否是數字
function chkNumeric(thisObj,strText,bEmpty,ValRang)
{
 var thisVal=thisObj.value;
 var thisText;
 
 if(bEmpty==undefined) bEmpty=false;
 if(bEmpty)
 {
  if(isEmpty(thisVal)) return true;
 }
 if(!isNumeric(thisVal))
 {
  if(strText==undefined) strText="";
  //strText+="非法數字,請輸入數字。";
  alert(strText);
  thisObj.focus();
  return false;
 }
 
 if(ValRang!=undefined && ValRang.length>0)
 {
  if(chkRange(thisVal,ValRang,strText))
   return true;
 }
  
 thisObj.focus();
 return false;
}

//判斷是否是大於0的數字
function chkUNumeric(thisObj,strText,bEmpty,ValRang)
{
 var thisVal=thisObj.value;
 
 if(bEmpty==undefined) bEmpty=true;
 if(bEmpty)
 {
  if(isEmpty(thisVal)) return true;
 }
 if(!isUNumeric(thisVal))
 {
  if(strText==undefined) strText="";
  //strText+="非法數字,請輸入大於0的數字。";
  alert(strText);
  thisObj.focus();
  return false;
 }
 if(chkRange(thisVal,ValRang,strText))
  return true;
  
 thisObj.focus();
 return false;
}

//判斷是否是整數
function chkInt(thisObj,strText,bEmpty,ValRang)
{
 var thisVal=thisObj.value;
 if(bEmpty==undefined) bEmpty=false;
 if(bEmpty)
 {
  if(isEmpty(thisVal)) return true;
 }
 if(!isInt(thisVal))
 {
  if(strText==undefined) strText="";
  //strText+="非法數字,請輸入整數。";
  alert(strText);
  thisObj.focus();
  return false;
 }
 if(chkRange(thisVal,ValRang,strText))
  return true;
 thisObj.focus();
 
 return false;
}

//判斷是否是大於0的整數
function chkUInt(thisObj,strText,bEmpty,ValRang)
{
 var thisVal=thisObj.value;
 if(bEmpty==undefined) bEmpty=false;
 if(bEmpty)
 {
  if(isEmpty(thisVal)) return true;
 }
 
 if(!isUInt(thisVal))
 {
  if(strText==undefined) strText="";
  //strText+="非法數字,請輸入大於0的整數。";
  alert(strText);
  thisObj.focus();
  return false;
 }
 if(chkRange(thisVal,ValRang,strText))
  return true;
  
 thisObj.focus();
 return false;
}

//判斷是否為空
function chkEmpty(thisObj,strText,DefVal)
{
 var thisVal=thisObj.value;
 if(isEmpty(thisVal))
 {
  //strText+="為空值,請輸入資料。";
  alert(strText);
  thisObj.focus();
  return false;
 }
 if(DefVal==null || DefVal==undefined || DefVal.length==0)
  return true;

 if(thisVal==DefVal)
 {
  alert(strText);
  thisObj.focus();
  return false;
 }
 return true;
}

//判斷數字範圍
function chkRange(objVal,ValRang,strText)
{
 if(ValRang==undefined || ValRang.length==0)
  return true;
  
 var arValRang=ValRang.split(",");
 
 if(arValRang.length<2) return true;
 
 if(strText==undefined) strText="";
 strText+="超出範圍(";
 
 var nVal= parseFloat(objVal);
 if(arValRang[0]=="*")
 {
  if(nVal<=parseFloat(arValRang[1])) return true;
  strText+="最大為"+arValRang[1];
 }
 else if(arValRang[1]=="*")
 {
  if(nVal>=parseFloat(arValRang[0])) return true;
  strText+="最小為"+arValRang[0];
 }
 else
 {
  if(nVal>=parseFloat(arValRang[0]) && nVal<=parseFloat(arValRang[1])) return true;
  strText+=arValRang[0]+"--"+arValRang[1];
 }
 strText+=")";
 alert(strText);
 return false;
}

///
//判斷日期
function chkDate(thisObj,strText,bEmpty,ValRang)
{
 var thisVal=thisObj.value;
 if(bEmpty==undefined) bEmpty=false;
 if(bEmpty)
 {
  if(isEmpty(thisVal)) return true;
 }
 if(!isDate(thisVal))
 {
  if(strText==undefined) strText="";
  //strText+="非法日期,請輸入正確的日期(2003-01-01)。";
  alert(strText);
  thisObj.focus();
  return false;
 }
 if(chkDateRange(thisVal,strText,ValRang))
  return true;
  
 thisObj.focus();
 return false;
}

//判斷日期範圍
function chkDateRange(objVal,strText,ValRang)
{
 if(ValRang==undefined || ValRang.length==0)
  return true;
 
 var bText=true;
 if(strText==undefined || strText==null)
 {
  strText="";
  bText=false;
 }
 
 objVal=objVal.replace( /-/g,"/");
 dtVal=new Date(objVal)
 
 var arValRang=ValRang.split(",");
 
 if(arValRang.length<2)
 {
  var dtDate=arValRang[0].replace( /-/g,"/");
  dtDate=new Date(dtDate);
  if(dtVal==dtDate) return true;
  if(!bText) strText="輸入的日期必須等於"+dtDate;
  alert(strText);
  return false;
 }
 
 if(arValRang[0]=="*")
 {
  var dtDate2=arValRang[1].replace( /-/g,"/");
  dtDate2=new Date(dtDate2);
  if(dtVal<=dtDate2) return true;
  if(!bText) strText+="最大為"+arValRang[1];
 }
 else if(arValRang[1]=="*")
 {
  var dtDate1=arValRang[0].replace( /-/g,"/");
  dtDate1=new Date(dtDate1);
  if(dtVal>=dtDate1) return true;
  if(!bText) strText+="最小為"+arValRang[0];
 }
 else
 {
  var dtDate1=arValRang[0].replace( /-/g,"/");
  dtDate1=new Date(dtDate1);
  var dtDate2=arValRang[1].replace( /-/g,"/");
  dtDate2=new Date(dtDate2);
  
  if(dtVal>=dtDate1 && dtVal<=dtDate2) return true;
  if(!bText) strText+=arValRang[0]+"--"+arValRang[1];
 }
 if(!bText) strText+=")";
 alert(strText);
 return false;
}

//
//檢查radio是否有選擇
function chkRadio(thisObj,strText,defIndex)
{
 if(thisObj!=null && thisObj!=undefined)
 {
  if(thisObj.length>0)
  {
   for(myChkIndex1=0;myChkIndex1<thisObj.length;myChkIndex1++)
   {
    if(thisObj[myChkIndex1].checked)
    {
     if(defIndex==null || defIndex==undefined) return true;
     if(myChkIndex1!=defIndex) return true;
    }
   }
  }
  else
  {
   if(thisObj.checked)
    return true;
  }
 }
 alert(strText);
 if(thisObj.length>0) thisObj[0].focus();
 else thisObj.focus();
 return false;
}

//檢查radio是否有選擇
function chkSelect(thisObj,strText,defIndex)
{
 if(thisObj!=null && thisObj!=undefined)
 {
  for(myChkIndex2=0;myChkIndex2<thisObj.options.length;myChkIndex2++)
  {
   if(thisObj.options[myChkIndex2].selected)
   {
    if(defIndex==null || defIndex==undefined) return true;
     if(myChkIndex2!=defIndex) return true;
   }
  }
 }
 alert(strText);
 thisObj.focus();
 return false;
}


//判斷字元
/*thisObj=需要判斷的元素名,即<input>的name值(如 form1.email)
strText=顯示的控制項的中文名稱(用於顯示提示)
bEmpty=true:允許為空(預設=true),即為空時不判斷*/
//判斷email格式
function chkEmail(thisObj,strText,bEmpty)
{
 var thisVal=thisObj.value;
 if(bEmpty==undefined) bEmpty=false;
 if(bEmpty)
 {
  if(isEmpty(thisVal)) return true;
 }
 
 if(!isEmail(thisVal))
 {
  if(strText==undefined) strText="";
  //strText+="Email格式不正確,請輸入形如:abc@site.com。";
  alert(strText);
  thisObj.focus();
  return false;
 }
 return true;
}

//判斷是否符合reg指定的格式
function chkReg(thisObj,reg,strText,bEmpty)
{
 var thisVal=thisObj.value;
 if(bEmpty==undefined) bEmpty=false;
 if(bEmpty)
 {
  if(isEmpty(thisVal)) return true;
 }
 
 if(!isReg(thisVal,reg))
 {
  if(strText==undefined) strText="";
  //strText+="Email格式不正確,請輸入形如:abc@site.com。";
  alert(strText);
  thisObj.focus();
  return false;
 }
 return true;
}

//判斷字符長度是否在指定范圍內
function chkLength(thisObj,strText,ValRang)
{
 var strVal=thisObj.value;
 if(isRange(strVal.length,ValRang)) return true;
 alert(strText);
 thisObj.focus();
 return false;
}
//
//格式化日期(去掉時間,在月日前加0,格式化後格式?:2003-01-01)
function FormatDate(strDate)
{
 if(strDate.length==0) return strDate;
 var iLen=strDate.indexOf(" ");
 if(iLen>0)
  strDate=strDate.substr(0,iLen);
 strDate=strDate.replace("-","/")
 
 var dtDate=new Date(strDate);
 var iTemp=dtDate.getYear();
 if(iTemp<100)
  strDate="19"+iTemp;
 else
  strDate=iTemp;
 var iTemp=dtDate.getMonth()+1; 
 if(iTemp<10)
  strDate+="-0"+iTemp.toString();
 else
  strDate+="-"+iTemp.toString();
 
 var iTemp=dtDate.getDate();
 if(iTemp<10)
  strDate+="-0"+iTemp.toString();
 else
  strDate+="-"+iTemp.toString();
 return strDate;
 
}

//
//身分證檢查(台灣)
function isIdCard(strVal)
{
 if(strVal==null || strVal==undefined || strVal.length==0)
  return false;
 //strVal=strVal.toUpperCase();
 var reg1=/^[A-Z]{1}[1-2]{1}/d{8}$/g;
 if(!isReg(strVal,reg1)) return false; //格式不對
 
 var strVal0=strVal.substr(0,1); //取第一位(字母)
 var strTemp="ABCDEFGHJKLMNPQRSTUVXYWZIO";
 var nVal0=strTemp.indexOf(strVal0)+10; //第一位字母對應的數值
 strVal=nVal0.toString()+strVal.substr(1,strVal.length-1); //變成11位身份證號

 nVal0=parseInt(strVal.substr(0,1))+parseInt(strVal.substr(strVal.length-1,1)); //第一位+最後一位
 //document.write(strVal+"<p>");
 var iStart=10;
 for(myChkIndex3=1;myChkIndex3<strVal.length-1;myChkIndex3++) //第2到第10位分別*(9,8,7,6,5,4,3,2,1),然後積相加
 {
  //document.write(strVal.substr(i,1)+"*"+(iStart-i)+"<br>");
  nVal0+=parseInt(strVal.substr(myChkIndex3,1))*(iStart-myChkIndex3);
 }
 //document.write(nVal0+"<p>")
 if(nVal0%10==0) return true; //能被10除,是身份證號,否則就不是
 else return false;
}

function chkIdCard(thisObj,strText)
{
 if(thisObj==null||thisObj==undefined)
 {
  alert(strText);
  return false;
 }
 var myVal=trim(thisObj.value);
 myVal=myVal.toUpperCase();
 if(!isIdCard(myVal))
 {
  alert(strText);
  thisObj.focus();
  return false;
 }
 thisObj.value=myVal;
 return true;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值