天天看點

一個不錯的 js 校驗

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:"國外"}

//此檔案提供WEB應用程式中常用的javascript方法。



/*

 *--------------- 用戶端表單通用驗證CheckForm(oForm) -----------------

 * 功能:通用驗證所有的表單元素.

 * 使用:

 *    <form name="form1" οnsubmit="return CheckForm(this)">

 *    <input type="text" name="id" check="^/S+___FCKpd___0quot; warning="id不能為空,且不能含有空格">

 *    <input type="submit">

 *    </form>

 * author:wanghr100(灰豆寶寶.net)

 * email:[email protected]

 * update:19:28 2004-8-23

 * 注意:寫正規表達式時一定要小心.不要讓"有心人"有空子鑽.

 * 已實作功能:

 * 對text,password,hidden,file,textarea,select,radio,checkbox進行合法性驗證

 * 待實作功能:把正則表式寫成個庫.

 *--------------- 用戶端表單通用驗證CheckForm(oForm) -----------------

 */





//主函數

function CheckForm(oForm)

{

    var els = oForm.elements;

    //周遊所有表元素

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

    {

        //是否需要驗證

        if(els[i].check)

        {

            //取得驗證的正則字元串

            var sReg = els[i].check;

            //取得表單的值,用通用取值函數

            var sVal = GetValue(els[i]);

            //字元串->正規表達式,不區分大小寫

            var reg = new RegExp(sReg,"i");

            if(!reg.test(sVal))

            {

                //驗證不通過,彈出提示warning

                alert(els[i].warning);

                //該表單元素取得焦點,用通用傳回函數

                GoBack(els[i])  

                return false;

            }

        }

    }

}



//通用取值函數分三類進行取值

//文本輸入框,直接取值el.value

//單多選,周遊所有選項取得被選中的個數傳回結果"00"表示選中兩個

//單多下拉菜單,周遊所有選項取得被選中的個數傳回結果"0"表示選中一個

function GetValue(el)

{

    //取得表單元素的類型

    var sType = el.type;

    switch(sType)

    {

        case "text":

        case "hidden":

        case "password":

        case "file":

        case "textarea": return el.value;

        case "checkbox":

        case "radio": return GetValueChoose(el);

        case "select-one":

        case "select-multiple": return GetValueSel(el);

    }

    //取得radio,checkbox的選中數,用"0"來表示選中的個數,我們寫正則的時候就可以通過0{1,}來表示選中個數

    function GetValueChoose(el)

    {

        var sValue = "";

        //取得第一個元素的name,搜尋這個元素組

        var tmpels = document.getElementsByName(el.name);

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

        {

            if(tmpels[i].checked)

            {

                sValue += "0";

            }

        }

        return sValue;

    }

    //取得select的選中數,用"0"來表示選中的個數,我們寫正則的時候就可以通過0{1,}來表示選中個數

    function GetValueSel(el)

    {

        var sValue = "";

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

        {

            //單選下拉框提示選項設定為value=""

            if(el.options[i].selected && el.options[i].value!="")

            {

                sValue += "0";

            }

        }

        return sValue;

    }

}



//通用傳回函數,驗證沒通過傳回的效果.分三類進行取值

//文本輸入框,光标定位在文本輸入框的末尾

//單多選,第一選項取得焦點

//單多下拉菜單,取得焦點

function GoBack(el)

{

    //取得表單元素的類型

    var sType = el.type;

    switch(sType)

    {

        case "text":

        case "hidden":

        case "password":

        case "file":

        case "textarea": el.focus();var rng = el.createTextRange(); rng.collapse(false); rng.select();

        case "checkbox":

        case "radio": var els = document.getElementsByName(el.name);els[0].focus();

        case "select-one":

        case "select-multiple":el.focus();

    }

}



//檢測所有控件輸入值的合法性



function checkAll(v_frm) {

 //var p_frm = document.forms[getNetuiTagName(v_frm)];

    var p_frm = v_frm;

 var eCount = p_frm.elements.length;

 var v="";

    

    var element_type = "";//控件類型,隻針對下拉框



 var discript = "";//輸入值名稱



 var nullflag = "1";//是否可為空值,0-不可,1-可

 var v_type = "s";//輸入值類型,s-字元串, i-整數,f-浮點數,d-日期,e-郵件,c-身份證,p-郵編,phone -電話号碼,m -行動電話,ip - ip位址,t - 短時間



 var length = "";//輸入值長度



 var mins = "";//最小輸入值



 var maxs = "";//最大輸入值





 for(var i = 0; i < eCount; i++) {

        

        element_type = "";

  discript = "";

  v_type = "s";

  length = "";

  nullflag = "1";

  maxs = "";

  mins = "";





  var e=p_frm.elements[i];

  if (e.style.behavior == null || e.style.behavior == "")

   continue;

  parseTag(e.style.behavior);

        if (( v = getNextValue()) != null) {

   element_type = v;

  }

  if (( v = getNextValue()) != null) {

   discript = v;

  }

  if ((v = getNextValue()) != null) {

   nullflag = v;

  }

  if ((v = getNextValue()) != null) {

   v_type = v;

  }

  if ((v = getNextValue()) != null) {

   length = v;

  }

  if ((v = getNextValue()) != null) {

   mins = eval(v);

  }

  if ((v = getNextValue()) != null) {

   maxs = eval(v);

  }

  if (checkValidate(element_type,e, discript, v_type, length, nullflag, maxs, mins) == "0")

   return false;

 }

 return true;

}

//判斷是否是純空格字元串

function isSpaceStr(str) {

 var e;

 var result = false;

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

 {

  e = str.charAt(i);

  if(e != " ")

  {

   result = true;

   break;

  }

 }

 return result;

}

//判斷是否是純回車

function isEnter(str)

      {

          

         var len=str.length;

         if(len<1) return false;

         for(var i=0;i<len;i++){   

            str=str.replace("/n"," ");

            str=str.replace("/r"," ");

         if(str.substring(i,i+1)!=" "){

                return true;

                  }

            }

          return false;

    }

/**

* 檢驗使用者指定的對象是否合法

* @param element        使用者需要校驗的對象

* @param discript       對象的描述



* @param type           對象的類型:

*                       i  整數

*                       f  浮點

*                       s  字元

*                       d  日期

*                       e  郵件

*                       c  身份證



*                       p  郵編

* @param length         對象的最大長度



* @param type           能否為空:



*                       1  可以為空

*                       0  不能為空

* @param maxs           最大值



* @param mins           最小值



* @return               全部條件合法傳回true;否則傳回false

*/

function checkValidate(element_type,element, discript, type, length, nullflag, maxs, mins) {

    if (element == "") {

        window.alert("函數調用出錯,請輸入控件!")

        return(0);

    }



    if (discript == "") {

        window.alert("函數調用出錯,請輸入控件描述!")

        return(0);

    }



    if (nullflag == 0) {

        if (element_type == "select") {

            if ( element.style.display=="block" || element.style.display=="") {

                if (element.value == "0") {

                    window.alert("請選擇" + discript + "!");

                    element.focus();

                    return(0);  

                }

            }

        } else {

            if (element.value == "" || !isSpaceStr(element.value) || !isEnter(element.value)) {

                window.alert("請輸入" + discript + "!");

                element.focus();

                return(0);

            }

        }

    }else

 {

  if(element.value=="")  

         return(1);

 }

    if (type != "") {

        switch (type) {

      case "i"://整數

       {

        if (element.value.length != 0 && isInteger(element.value) != true) {

         window.alert(discript + "必須輸入整數!")

         element.focus();

         return(0);

        }else if (element.value.length == 0 ){

            //element.value = "0" ;

        }

        break;

       }



      case "f"://浮點數



       {

        if (element.value.length != 0 && isNaN(element.value) == true) {

               window.alert(discript + "必須輸入數字!")

         element.focus();

         return(0);

        } if (element.value.length == 0 ){

            //element.value = "0" ;

        }

        break;

       }

      case "s"://字元串



       {

        break;

       }

      case "d"://日期

       {

        if (element.value.length != 0 && isDate(element.value) == false) {

               window.alert(discript + "必須輸入有效日期!(日期格式:yyyy-mm-dd)")

         element.focus();

         return(0);

        }

           break;

       }

         case "e"://郵件

       {

        if (element.value.length != 0 && isEmail(element.value) == false) {

               window.alert("請輸入有效的" + discript + "!")

         element.focus();

         return(0);

        }

           break;

       }

      case "c"://身份證



       {

        if (element.value.length != 0 && cidInfo(element.value) == false) {

               window.alert("請輸入有效的" + discript + "!")

         element.focus();

         return(0);

        }

           break;

       }

      case "p"://郵編

       {

        if (element.value.length != 0 && isPostCode(element.value) == false) {

               window.alert("請輸入有效的" + discript + "!")

         element.focus();

         return(0);

        }

           break;

       }

   case "m"://行動電話

       {

        if (element.value.length != 0 && isMoveTel(element.value) == false) {

               window.alert("請輸入有效的" + discript + "!")

         element.focus();

         return(0);

        }

           break;

       }

   case "ip"://ip位址

       {

        if (element.value.length != 0 && isIP3(element.value) == false) {

               window.alert("請輸入有效的" + discript + "!")

         element.focus();

         return(0);

        }

           break;

       }

   case "t"://短時間 (13:04:15)

       {

        if (element.value.length != 0 && isShortTime(element.value) == false) {

               window.alert("請輸入有效的" + discript + "!")

         element.focus();

         return(0);

        }

           break;

       }

   case "hour"://小時 

       {

        if (element.value.length != 0 && isHoutTime(element.value) == false) {

               window.alert("請輸入有效的" + discript + "格式!")

         element.focus();

         return(0);

        }

           break;

       }    

   case "minute"://分鐘 

       {

        if (element.value.length != 0 && isMinuteTime(element.value) == false) {

               window.alert("請輸入有效的" + discript + "格式!")

         element.focus();

         return(0);

        }

           break;

       }           

   case "phone"://電話号碼

       {

        if (element.value.length != 0 && PhonesCheck(element.value) == false) {

               window.alert("請輸入有效的" + discript + "!")

         element.focus();

         return(0);

        }

           break;

       }

      case "quhao"://電話号碼(區号+電話)

       {

        if (element.value.length != 0 && PhonesQHCheck(element.value) == false) {

               window.alert("請輸入有效的" + discript + "! (格式:區号-電話)")

         element.focus();

         return(0);

        }

           break;

       } 

      case "simpleP"://簡單電話号碼(數字,'(',')','-')

       {

        if (element.value.length != 0 && SimplePhone(element.value) == false) {

               window.alert("請輸入有效的" + discript + "!")

         element.focus();

         return(0);

        }

           break;

       }       

       

     }

    }



    if (length != "" && type != "d") {

        if (lengthCheck(element.value, length)) {

         window.alert(discript + "的長度必須小于" + length/2 + "個漢字!" + length + "個字元!");

         element.focus();

         return(0);

     }

    }



    if (maxs != "" && mins != "") 

 {

  if(type =="s")

  {

   if (lenInFact(element)<mins || lenInFact(element)>maxs) 

   {

    window.alert(discript + "的長度必須小于" + maxs/2 + "個漢字!" + maxs + "個字元, 大于" + mins/2 + "個漢字!" + mins + "個字元!");

    element.focus();

    return(0);

   }

  }

  else

  {

   if (element.value < mins || element.value > maxs) 

   {

    window.alert(discript + "的值必須小于" + maxs + ", 大于" + mins + "!");

    element.focus();

    return(0);

   }

  }

    } 

 else 

 {

        if (maxs != "") 

  {

   if(type =="s")

   {

          if (lenInFact(element)> maxs) 

    {

              window.alert(discript + "的長度必須小于" + maxs/2 + "個漢字" + maxs + "個字元!");

           element.focus();

           return(0);

          }

      }

      else

      {

       if (element.value > maxs) 

    {

              window.alert(discript + "的值必須小于" + maxs + "!");

           element.focus();

           return(0);

          }

      }

      

        }

        if (mins != "") 

  {

   if(type =="s")

   {

          if (lenInFact(element) < mins) 

    {

              window.alert(discript + "的長度必須大于" +mins/2 + "個漢字" + mins + "個字元!");

           element.focus();

           return(0);

          }

      }

      else

      {

       if (element.value < mins) 

    {

              window.alert(discript + "的值必須大于" + mins + "!");

           element.focus();

           return(0);

          }

      }

        }

    }



    return(1);

}

//電話号碼的驗證



//要求:

  //(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}$)

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}$)/

 

 //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}1[3,5][0-9]{9}$)/ 

 

 if(reg.test(str)){

  return true;

 }

 else{

  return false;

 }

}



function PhonesCheck(s) {

 var str=s;

 var reg=/(^(/([/d]{3,4}/)|[/d]{3,4}/-){0,1}[/d]{3,8}(/([/d]{1,4}/)|/-[/d]{1,4}){0,1}$)|(^0{0,1}13[/d]{9}$)|(^0{0,1}159[/d]{8}$)/;

 

 //var reg=/(^(/([/d]{3,4}/)|[/d]{3,4}/-){0,1}[/d]{3,8}(/([/d]{1,4}/)|/-[/d]{1,4}){0,1}$)|(^0{0,1}1[3,5][/d]{9}$)/;

  

 if(reg.test(str)){

  return true;

 }

 else{

  return false;

 }

}



function PhonesQHCheck(s) {

 var str=s;

 

 var reg=/(^[0-9]{3,4}/-[0-9]{3,8}$)/

  

 if(reg.test(str)){

  return true;

 }

 else{

  return false;

 }

}



function SimplePhone(str){

 var count;

 var numflag;

 for(count = 0; count < str.length; count++){

  numchar = str.charAt(count);

  numflag = 0;

        if(numchar == "(" || numchar == ")" || numchar == "-" ){

   numflag = 1;

  }else{

   numvalue = numchar - '0';

      if (numvalue >= 0 && numvalue <= 9){

    numflag = 1;

   }

  }

  if(numflag == 0){

   return false;

  }

 }

 return true;

}



 





//行動電話(手機)

//樣式:13531214732或013531214732

function isMoveTel(elem){

 var pattern=/(^0{0,1}13[0-9]{9}$)|(^0{0,1}159[/d]{8}$)/;

 if(pattern.test(elem)){

  return true;

 }else{

  //elert("電話号碼不正确");

  return false;

 }

}



//驗證IP位址

function isIP3(elem){

  var pattern=/(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])/.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)/.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)/.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])/;

  if(pattern.test(elem)){

    return true;

  }else{

 //alert("ip格式不正确");

    return false;

  }

 }



//短時間(形如:  13:04:06)

//更具彈性的正規表達式:/^(/d{1,4})(-|//)(/d{1,2})/2(/d{1,2})$/

function isShortTime(elem){

 var pattern=/^([0-9]{1,2})(:)([0-9]{1,2})(:)([0-9]{1,2})$/;

 var temp=elem.match(pattern);

 if(temp==null)

  return false;

 if(temp[1]>=24)

  return false;

 if(temp[3]>=60)

  return false;

 if(temp[5]>=60)

  return false;

 return true;

}

function isHoutTime(elem){

 var pattern=/^([0-9]{1,2})$/;

 var temp=elem.match(pattern);

 if(temp==null)

  return false;

 if(temp[1]>=24)

  return false;

 return true;

}

function isMinuteTime(elem){

 var pattern=/^([0-9]{1,2})$/;

 var temp=elem.match(pattern);

 if(temp==null)

  return false;

 if(temp[1]>=60)

  return false;

 return true;

}

//判斷是否整數

function isInteger(integer) {

    var count;

    var numchar;

    var numvalue;

    for (count = 0; count < integer.length; count++) {

     numchar = integer.charAt(count);

     numvalue = numchar - '0';

     if (!(numvalue >= 0 && numvalue <= 9))

      return false;

    }

    return true;

}



//判斷是否有效郵件

function isEmail(mail) {

    if (killSpace(mail) == "") return false;



 var v_email = mail.substring(mail.indexOf("@") + 1)

 if ( (mail.indexOf("@") == -1) ||

              (mail.indexOf("@") == 0)  ||

               (mail.indexOf("@") != mail.lastIndexOf("@")) ||

                 (v_email.indexOf(".") == -1)  ||

                    (v_email.indexOf(".") == 0) ||

                     ((mail.indexOf(".") + 1) == mail.length) ) {

  return false;

 }

 return true;

}



//身份證簡單驗證,單純的位數驗證

function isIdCard(idcard){

 var re = /^[/d]{15}|[/d]{18}|[/d]{17}[Xx]$/;

 if(re.test(idcard)){

  //alert("驗證通過!");

  return true;

 }

 else{

  //alert("身份證号碼錯誤!");

  return false;

 }

}



//身份證嚴格驗證,包括地區驗證、生日驗證和校驗位驗證

//18位公民身份證号碼的編排規則:

//身份号碼是特征組合碼,由十七位數字本體碼和一位校驗碼組成。

//排列順序從左至右依次為:六位數字位址碼,八位數字出生日期碼,三位數字順序碼和一位校驗碼。其含義如下:

//1. 位址碼:表示編碼對象常住戶口所在縣(市、旗、區)的行政區劃代碼。(本函數隻對省級地區進行檢驗)

//2. 出生日期碼:表示編碼對象出生的年、月、日,年、月、日分别用4位、2位、2位數字表示,之間不用分隔符。(出生日期合法性檢驗)

//3. 順序碼:表示在同一位址碼所辨別的區域範圍内,對同年、同月、同日出生的人編定的順序号,順序碼的奇數配置設定給男性,偶數配置設定給女性。

//校驗的計算方式:

//  1. 對前17位數字本體碼權重求和

//  公式為:S = Sum(Ai * Wi), i = 0, ... , 16

//  其中Ai表示第i位置上的身份證号碼數字值,Wi表示第i位置上的權重因子,其各位對應的值依次為: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2

//  2. 以11對計算結果取模

//  Y = mod(S, 11)

//  3. 根據模的值得到對應的校驗碼

//  對應關系為:

//     Y值: 0 1 2 3 4 5 6 7 8 9 10

//  校驗碼: 1 0 X 9 8 7 6 5 4 3 2

function cidInfo(idcard){

 var Errors = new Array("驗證通過!","身份證号碼位數不對!","身份證号碼出生日期超出範圍或含有非法字元!","身份證号碼校驗錯誤!","身份證地區非法!");

 var area = {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:"國外"}

 var idcard,Y,JYM;

 var S,M;

 var idcard_array = new Array();

 idcard_array = idcard.split("");



 //地區檢驗

 if(area[parseInt(idcard.substr(0,2))]==null) {

  //alert(Errors[4]);

  return false;

 }



 //身份号碼位數、出生日期的合法性檢查及格式檢驗

 switch(idcard.length){

  case 15:

   //15位身份号碼檢測

   if ( (parseInt(idcard.substr(6,2))+1900) % 4 == 0 || ((parseInt(idcard.substr(6,2))+1900) % 100 == 0 && (parseInt(idcard.substr(6,2))+1900) % 4 == 0 )){

    ereg=/^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/;//閏年出生日期的合法性正規表達式

   } 

   else {

    ereg=/^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/;//平年出生日期的合法性正規表達式

   }



   if(ereg.test(idcard)){

    //alert(Errors[0]);

    return true;

   }

   else{ 

    //alert(Errors[2]);

    return false;

   }

   break;

  

  case 18:

   //18位身份号碼檢測

   if ( parseInt(idcard.substr(6,4)) % 4 == 0 || (parseInt(idcard.substr(6,4)) % 100 == 0 && parseInt(idcard.substr(6,4))%4 == 0 )){

    ereg=/^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/;//閏年出生日期的合法性正規表達式

   } 

   else {

    ereg=/^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/;//平年出生日期的合法性正規表達式

   }



   //測試出生日期的合法性

   if(ereg.test(idcard)){

    //計算校驗位

    S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7 + (parseInt(idcard_array[1]) + parseInt(idcard_array[11])) * 9 + (parseInt(idcard_array[2]) + parseInt(idcard_array[12])) * 10 + (parseInt(idcard_array[3]) + parseInt(idcard_array[13])) * 5 + (parseInt(idcard_array[4]) + parseInt(idcard_array[14])) * 8 + (parseInt(idcard_array[5]) + parseInt(idcard_array[15])) * 4 + (parseInt(idcard_array[6]) + parseInt(idcard_array[16])) * 2 + parseInt(idcard_array[7]) * 1 + parseInt(idcard_array[8]) * 6 + parseInt(idcard_array[9]) * 3 ;

    Y = S % 11;

    M = "F";

    JYM = "10X98765432";

    M = JYM.substr(Y,1);

    

    //判斷校驗位

    if(M == idcard_array[17].toUpperCase()){ 

     //alert(Errors[0]); 

     return true;

    }

    else{

     //alert(Errors[3]);

     return false;

    }

   }

   else{

    //alert(Errors[2]);

    return false;

   }

   break;



  default:

   //alert(Errors[1]);

   return false;

   break;

 }

}





//判斷是否為郵編



function isPostCode(postCode) {

    var v_postCode = killSpace(postCode);

    if (v_postCode == "") return false;



    if (v_postCode.length != 6) {

        //alert("郵政編碼必須為6位數字!");

        return false;

    }



    if (!isInteger(v_postCode)) {

        //alert("郵政編碼必須全部為數字!");

        return false;

    }else {

        return true;

    }

}



/**

* 調用此函數校驗日期。



* @param s         輸入日期string s,如2001-01-01

* @return          若輸入的日期沒有包含非法字元,以及該日期合法,則傳回true;否則傳回false

*/

function isDate(s) {

    if (s.length != 10 || (s.charAt(4) != '-' && s.charAt(4) != '/') || (s.charAt(7) != '-' && s.charAt(7) != '/'))

     return false;

    if (!isInteger(s.substring(0, 4)) || !isInteger(s.substring(5, 7)) || !isInteger(s.substring(8, 10)) || eval(s.substring(0, 4)) < 1 || eval(s.substring(5, 7)) < 1 || eval(s.substring(8, 10)) < 1)

     return false;

    if (checkDate(s.substring(0, 4), s.substring(5, 7), s.substring(8, 10)) == false)

     return false;

    return true;

}



//檢驗日期是否合法



function checkDate(year, month, day) {

    var iyear;

    var imonth;

    var iday;

    if(year.length != 4 || month.length != 2 || day.length != 2)

     return false;

    if (!isInteger(year) || !isInteger(month) || !isInteger(day))

     return false;

    iyear = getValueOfInt(year);

    imonth = getValueOfInt(month);

    iday = getValueOfInt(day);

    if (imonth < 1 || imonth > 12) return false;

    switch(imonth) {

     case 1:

     case 3:

     case 5:

     case 7:

     case 8:

     case 10:

     case 12:

      if (iday > 31) return false;

       break;

     case 4:

     case 6:

     case 9:

     case 11:

      if (iday > 30) return false;

      break;

     default:

      if(mod(iyear, 4) == 0 && (mod(iyear, 100) != 0 || mod(iyear, 400) == 0)) {//判斷是否潤年

       if (iday > 29) return false;

      }else {

       if (iday > 28) return false;

      }

    }

 return true;

}



//取得輸入字元串所代表的整數值



function getValueOfInt(string) {

    var count;

    var numchar;

    var numvalue;

    var value;

    value = 0;

    for ( count = 0; count < string.length; count++) {

     numchar = string.charAt(count);

     numvalue = numchar - '0';

     value = value * 10 + numvalue;

    }

    return value;

}



var e_tag; //目前正在分拆得Tag值





//開始解析分拆Tag值



function parseTag(p_tag) {

 e_tag = p_tag.substring (p_tag.indexOf("'")+1,p_tag.lastIndexOf ("'"));

}



//得到下一個分拆處理的值



function getNextValue() {

 if(e_tag == null || e_tag == "")

  return null;

 var p = e_tag.indexOf("|");

 if(p == -1)

  p = e_tag.length;

 var r = e_tag.substring(0,p);

 e_tag = e_tag.substring(p + 1, e_tag.length);

 return r;

}



//删除空格處理,供checkValidate調用

function killSpace(x) {

    while ((x.length > 0) && (x.charAt(0) == ' '))

     x = x.substring(1, x.length)

    while ((x.length > 0) && (x.charAt(x.length - 1) == ' '))

     x = x.substring(0, x.length - 1)

    return x;

}



//計算中文長度

function count_char(str) {

 var len = 0;

 for(i = 0; i < str.length; i++) {

  var ech = escape(str.charAt(i));

  if ( ech.length > 4 ){

//   len++;



 修改下面的數字,len + 1 表示2個字元代表一個中文字,len + 5 表示6個字元代表一個中文字

   len = len + 1;



/*

   if (ech>"%u07ff")

    len++;

*/

  }

  len ++;

 }

 return len;

}



//檢查輸入的内容是否超過指定的長度



function lengthCheck(text, size) {

 var len = count_char(text);

 if ( len > size ) {

        return true;

 }

 return false;

}





function mod(var1,var2) {

    return (var1%var2);

}





function getNetuiTagName(id) {

   return netui_names[id];

}



function getNetuiTagName(id, tag)

{

   var scopeId = getScopeId(tag);

   if (scopeId == "")

      return netui_names[id];

   else

      return netui_names[scopeId  + "__" + id];

}



function getScopeId(tag)

{

   if (tag == null)

      return "";

   if (tag.scopeId != null)

      return tag.scopeId;

   return getScopeId(tag.parentElement);

}



function disableAllButton(p_frm) {

 var eCount = p_frm.elements.length;

 for(var i = 0; i < eCount; i++) {

  var e=p_frm.elements[i];

  if (e.type=="button" || e.type=="submit"){

   e.disabled=true;

  }

 }

 return true;

}

function checkNumber(obj,num){ //過濾掉數字以外的鍵,48-57是數字鍵,46是".",num是小數點後幾位



    if (event.keyCode==46){

        if (obj.value.length<=0){ //如果第一個鍵是".",屏蔽掉



            event.keyCode=0;

   return;

            }//end if

        else {

            for (var n=0;n<obj.value.length;n++){ //如果"."過多,屏蔽掉



                if (obj.value.substring(n,n+1)=="."){

                    event.keyCode=0;

     

                    return;

                    }//end if

                }//end for n

            return ;

            }//end else

        }//end if

    if (event.keyCode<48 || event.keyCode>57){  //過濾其他鍵



        event.keyCode=0;

  return;

        }

    else{                                        //控制小數點後位數

        for (var n=0;n<obj.value.length;n++){

            if (obj.value.substring(n,n+1)=="."){

                if ((obj.value.length-n)>num){

                    event.keyCode=0;

        return;     

                    }//end if

                }//end if

            }//end for n

        }

}

function checkBer(obj,num){   //控制字元輸入長度

   for (var n=0;n<obj.value.length;n++){

                if ((obj.value.length-n)>=num){

                    event.keyCode=0;

                    return;

                    }//end if

            }//end for n

}



function lenInFact(obj,maxLen)

{

    var c;var len=0;var j=0;

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

    {

       c=escape(obj.value.charAt(i));   

       if(c.length>4)    

       {

         len=len+2;

       }

       else

       {

         len++;

       }

       

       if(len>maxLen)    

       {

          j=i-1;

          break;

       }

    }

    return j;

}



function lenInFact(obj)

{

    var c;

    var len=0;

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

    {

       c=escape(obj.value.charAt(i));   

       if(c.length>4)    

       {

         len=len+2;

       }

       else

       {

         len++;

       }

    }

    return len;

}

//區間日期檢查

function checkdate(date1,date2)

{

 if(date1.value != "" && date2.value !="" && date1.value > date2.value)

    {

        alert("起始時間應小于或等于截止時間!");

        return false;

    }

 return true;

}





function checkTextLength(obj,num){   //控制TextArea輸入長度

    //alert(obj.value.length);

    var maxLen=lenInFact(obj,num);

    //alert(maxLen);

    if(maxLen!=0)      

    {

      obj.value=obj.value.substring(0,maxLen);           

    }           

}



function checkMaxInput(form,maxLen,message,remLen) {

if (form.message.value.length > maxLen)

form.message.value = form.message.value.substring(0, maxLen);

else form.remLen.value = maxLen - form.message.value.length;

}



function checkInput(message,maxLen) {

if (message.value.length > maxLen) // if too long.... trim it!

message.value = message.value.substring(0, maxLen);

// otherwise, update 'characters left' counter

//else remLen.value = maxLen - message.value.length;

}

function checkMaxInput2(message,maxLen,remLen) {

if (message.value.length > maxLen)

message.value = message.value.substring(0, maxLen);

else remLen.value = maxLen - message.value.length;

}

//檢查時間範圍

function checkDateRange(y1,y2)

{

  

 d1 = getStringToDate(y1.value);

 d2 = getStringToDate(y2.value);

 d1m = d1.getTime();

 d2m = d2.getTime();

 if( d1m > d2m )

 {

  alert("起始時間不能大于終止時間,請重新輸入!");

  y1.value = "";

  y2.value = "";

  y1.focus();

  return false;

 }

 return true;

}

function getStringToDate(y1)

{

 year1 = y1.substring(0,y1.indexOf('-'));

 tmpY1 = y1.substring(y1.indexOf('-') + 1,y1.length);

 month1 = tmpY1.substring(0,tmpY1.indexOf('-'));

 day1 = tmpY1.substring(tmpY1.indexOf('-') + 1, tmpY1.length);

 return new Date(year1, month1, day1, 0, 0, 0, 0);

}

function processSubmit(obj){

  obj.disabled=true;  

  if(checkAll(obj.form)){ 

    obj.form.submit();

  }else {

    obj.disabled=false;

  }

} 

      

繼續閱讀