zoukankan      html  css  js  c++  java
  • JAVASCRIPT共通関数日付の妥当性チェック

    日付の妥当性チェック
    DateChk,LastDayChk,YearChkと3つのパートに分かれます。
    あまりJavaScript側でチェックする事はありませんが、どうしてもという場合はどうぞ。

    /**********************************************************************************************/
    //日付の妥当性チェック(YYYYMMDD形式でい引数は渡す事。)
    function DateChk(in_data){
    
      var chkData = in_data;
    
      //8桁以外はエラー
      if (chkData.length != 8)
         {return false;}
    
      //小数点が入っていたらエラー
      if (chkData.indexOf(".") != -1)
         {return false;}
    
      //マイナスが入っていたらエラー
      if (chkData.indexOf("-") != -1)
         {return false;}
    
      //数値チェック
      if (isNaN(chkData) == true)
         {return false;}
    
      in_Year = eval(chkData.substring(0,4));
      in_Month = eval(chkData.substring(4,6));
      in_Day = eval(chkData.substring(6,8));
    
      if ((in_Month>12) || (in_Day >31))
         {return false;}
    
      //閏年
      if ((in_Month == 2) && YearChk(in_Year))
         {in_Month=13;}
    
      //末日チェック
      if (LastDayChk(in_Month,in_Day))
         {return true;}
      else
         {return false;}
    
    }
    
    //末日チェック
    function LastDayChk(in_Month,in_Day){
      lastDay = new Array(31,28,31,30,31,30,31,31,30,31,30,31,29);
      if (lastDay[in_Month-1] >= in_Day)
         {return true;}
       else
         {return false;}
    }
    
    //年チェック
    function YearChk(in_Year){
      if ((in_Year % 4) == 0 && ((in_Year % 100) != 0 || (in_Year % 400)))
         {return true;}
      return false;
    } 
    
    /**********************************************************************************************/
  • 相关阅读:
    HIDS逐渐的成为主流 java程序员
    怎样做反向域名解析(反向DNS解析)? java程序员
    入侵检测系统的性能的辨别(2) java程序员
    Codeforces Round #146 (Div. 2)
    usaco1.34Prime Cryptarithm
    poj3667 hotel(线段树区间合并)
    poj1330Nearest Common Ancestors(水LCA)
    hdu4135Coprime(容斥原理)
    hdu1541Stars(树状数组)
    usaco 1.43Arithmetic Progressions
  • 原文地址:https://www.cnblogs.com/aggavara/p/2716236.html
Copyright © 2011-2022 走看看