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;
} 

/**********************************************************************************************/
原文地址:https://www.cnblogs.com/aggavara/p/2716236.html