2011-06-17 10 views
2

http://www.codingforums.com/archive/index.php/t-98569.htmlによれば、日付検証はJavaScriptの日付オブジェクトを使用して行うことができます。日付オブジェクトを使用したjavascriptの日付検証

私は年、月、日が別々のテキストボックスにあり、正規表現で妥当性が確認されています。今、JSの日付オブジェクトを利用して、 RegExpの検証では行われない閏年の妥当性検証。しかし、上記のリンクで議論されていることは、私のためにはうまくいかない。 たとえば、

<script type="text/javascript"> 
    var dy= new Date(2001,2,30); 
    alert(dy.getFullYear()+" "+dy.getMonth()+" "+dy.getDate()); 
</script> 

はそのまま日付を返します。 dy.getMonth()は-1のような何かを返して、2月の30日が存在しないことを示してはいけませんか? Dateオブジェクトを使用してこれを行うことができない場合は、日付を検証するためのいくつかの代替案を提案してください。

更新: 同じことを言う別のリンク(http://internotredici.com/article/checkdateinjavascript/)が見つかりました。 PS:かなり古いJS1.2に取り組んでいます。 JSで

答えて

6

あなたは日、月、年のビットから日付を作ることができ、その後、ビットが正しいことを確認します - 自分で..

は、これはあなたを助けることができます。

function isvalid_mdy(s){ 
    var day, A= s.split(/\D+/).map(function(itm,i){return parseInt(itm,10)}); 
    A[0]-=1; 
    try{ 
     day= new Date(A[2], A[0], A[1]); 
     if(day.getMonth()== A[0] && day.getDate()== A[1]) return day; 
     throw new Error('Bad Date '); 
    } 
    catch(er){ 
     return er.message; 
    } 
} 
function isvalid_dmy(s){ 
    var day, A= s.split(/\D+/).map(function(itm,i){return parseInt(itm,10)}); 
    A[1]-=1; 
    try{ 
     day= new Date(A[2], A[1], A[0]); 
     if(day.getMonth()== A[1] && day.getDate()== A[0]) return day; 
     throw new Error('Bad Date '); 
    } 
    catch(er){ 
     return er.message; 
    } 
} 
+0

ありがとうケネベック。私はこれを調べます。 – komedit1

1

、monhtは0で始まり、その0 =月、1 = 2月、=マル

30,1 2は(2月30日)3月2日

になりますあなたがすることによってこれをテストする必要がありますvalidate-date

+0

ありがとう!それはそれを説明する。しかし、リンク先のサイトには、正規表現の検証自体に欠陥があるようです。それは日付の42/42/4242のような値を受け入れます。 – komedit1

+0

私はこれが動作すると思う.. http://www.mkyong.com/regular-expressions/how-to-validate-date-with-regular-expression/ –

2

これは一部に役立つ場合があります。日付のバリデーションを実行するいくつかの方法を書いています。いくつかの良い方法があるかもしれませんが、私はこれらを考え出しました。これは、ロケールベースの形式の日付の検証に役立ちます。 注:入力フィールドに入力されている日付形式と日付文字列が手に入ります。

<script type="text/javascript"> 
     function validate() { 

      var format = 'yyyy-MM-dd'; 

      if(isAfterCurrentDate(document.getElementById('start').value, format)) { 
       alert('Date is after the current date.'); 
      } else { 
       alert('Date is not after the current date.'); 
      } 
      if(isBeforeCurrentDate(document.getElementById('start').value, format)) { 
       alert('Date is before current date.'); 
      } else { 
       alert('Date is not before current date.'); 
      } 
      if(isCurrentDate(document.getElementById('start').value, format)) { 
       alert('Date is current date.'); 
      } else { 
       alert('Date is not a current date.'); 
      } 
      if (isBefore(document.getElementById('start').value, document.getElementById('end').value, format)) { 
       alert('Start/Effective Date cannot be greater than End/Expiration Date'); 
      } else { 
       alert('Valid dates...'); 
      } 
      if (isAfter(document.getElementById('start').value, document.getElementById('end').value, format)) { 
       alert('End/Expiration Date cannot be less than Start/Effective Date'); 
      } else { 
       alert('Valid dates...'); 
      } 
      if (isEquals(document.getElementById('start').value, document.getElementById('end').value, format)) { 
       alert('Dates are equals...'); 
      } else { 
       alert('Dates are not equals...'); 
      } 
      if (isDate(document.getElementById('start').value, format)) { 
       alert('Is valid date...'); 
      } else { 
       alert('Is invalid date...'); 
      } 
     } 

     /** 
     * This method gets the year index from the supplied format 
     */ 
     function getYearIndex(format) { 

      var tokens = splitDateFormat(format); 

      if (tokens[0] === 'YYYY' 
        || tokens[0] === 'yyyy') { 
       return 0; 
      } else if (tokens[1]=== 'YYYY' 
        || tokens[1] === 'yyyy') { 
       return 1; 
      } else if (tokens[2] === 'YYYY' 
        || tokens[2] === 'yyyy') { 
       return 2; 
      } 
      // Returning the default value as -1 
      return -1; 
     } 

     /** 
     * This method returns the year string located at the supplied index 
     */ 
     function getYear(date, index) { 

      var tokens = splitDateFormat(date); 
      return tokens[index]; 
     } 

     /** 
     * This method gets the month index from the supplied format 
     */ 
     function getMonthIndex(format) { 

      var tokens = splitDateFormat(format); 

      if (tokens[0] === 'MM' 
        || tokens[0] === 'mm') { 
       return 0; 
      } else if (tokens[1] === 'MM' 
        || tokens[1] === 'mm') { 
       return 1; 
      } else if (tokens[2] === 'MM' 
        || tokens[2] === 'mm') { 
       return 2; 
      } 
      // Returning the default value as -1 
      return -1; 
     } 

     /** 
     * This method returns the month string located at the supplied index 
     */ 
     function getMonth(date, index) { 

      var tokens = splitDateFormat(date); 
      return tokens[index]; 
     } 

     /** 
     * This method gets the date index from the supplied format 
     */ 
     function getDateIndex(format) { 

      var tokens = splitDateFormat(format); 

      if (tokens[0] === 'DD' 
        || tokens[0] === 'dd') { 
       return 0; 
      } else if (tokens[1] === 'DD' 
        || tokens[1] === 'dd') { 
       return 1; 
      } else if (tokens[2] === 'DD' 
        || tokens[2] === 'dd') { 
       return 2; 
      } 
      // Returning the default value as -1 
      return -1; 
     } 

     /** 
     * This method returns the date string located at the supplied index 
     */ 
     function getDate(date, index) { 

      var tokens = splitDateFormat(date); 
      return tokens[index]; 
     } 

     /** 
     * This method returns true if date1 is before date2 else return false 
     */ 
     function isBefore(date1, date2, format) { 
      // Validating if date1 date is greater than the date2 date 
      if (new Date(getYear(date1, getYearIndex(format)), 
        getMonth(date1, getMonthIndex(format)) - 1, 
        getDate(date1, getDateIndex(format))).getTime() 
       > new Date(getYear(date2, getYearIndex(format)), 
        getMonth(date2, getMonthIndex(format)) - 1, 
        getDate(date2, getDateIndex(format))).getTime()) { 
       return true; 
      } 
      return false;     
     } 

     /** 
     * This method returns true if date1 is after date2 else return false 
     */ 
     function isAfter(date1, date2, format) { 
      // Validating if date2 date is less than the date1 date 
      if (new Date(getYear(date2, getYearIndex(format)), 
        getMonth(date2, getMonthIndex(format)) - 1, 
        getDate(date2, getDateIndex(format))).getTime() 
       < new Date(getYear(date1, getYearIndex(format)), 
        getMonth(date1, getMonthIndex(format)) - 1, 
        getDate(date1, getDateIndex(format))).getTime() 
       ) { 
       return true; 
      } 
      return false;     
     } 

     /** 
     * This method returns true if date1 is equals to date2 else return false 
     */ 
     function isEquals(date1, date2, format) { 
      // Validating if date1 date is equals to the date2 date 
      if (new Date(getYear(date1, getYearIndex(format)), 
        getMonth(date1, getMonthIndex(format)) - 1, 
        getDate(date1, getDateIndex(format))).getTime() 
       === new Date(getYear(date2, getYearIndex(format)), 
        getMonth(date2, getMonthIndex(format)) - 1, 
        getDate(date2, getDateIndex(format))).getTime()) { 
       return true; 
      } 
      return false; 
     } 

     /** 
     * This method validates and returns true if the supplied date is 
     * equals to the current date. 
     */ 
     function isCurrentDate(date, format) { 
      // Validating if the supplied date is the current date 
      if (new Date(getYear(date, getYearIndex(format)), 
        getMonth(date, getMonthIndex(format)) - 1, 
        getDate(date, getDateIndex(format))).getTime() 
       === new Date(new Date().getFullYear(), 
         new Date().getMonth(), 
         new Date().getDate()).getTime()) { 
       return true; 
      } 
      return false;     
     } 

     /** 
     * This method validates and returns true if the supplied date value 
     * is before the current date. 
     */ 
     function isBeforeCurrentDate(date, format) { 
      // Validating if the supplied date is before the current date 
      if (new Date(getYear(date, getYearIndex(format)), 
        getMonth(date, getMonthIndex(format)) - 1, 
        getDate(date, getDateIndex(format))).getTime() 
       < new Date(new Date().getFullYear(), 
         new Date().getMonth(), 
         new Date().getDate()).getTime()) { 
       return true; 
      } 
      return false;     
     } 

     /** 
     * This method validates and returns true if the supplied date value 
     * is after the current date. 
     */ 
     function isAfterCurrentDate(date, format) { 
      // Validating if the supplied date is before the current date 
      if (new Date(getYear(date, getYearIndex(format)), 
        getMonth(date, getMonthIndex(format)) - 1, 
        getDate(date, getDateIndex(format))).getTime() 
       > new Date(new Date().getFullYear(), 
         new Date().getMonth(), 
         new Date().getDate()).getTime()) { 
       return true; 
      } 
      return false;     
     } 

     /** 
     * This method splits the supplied date OR format based 
     * on non alpha numeric characters in the supplied string. 
     */ 
     function splitDateFormat(dateFormat) { 
      // Spliting the supplied string based on non characters 
      return dateFormat.split(/\W/); 
     } 

     /* 
     * This method validates if the supplied value is a valid date. 
     */ 
     function isDate(date, format) {     
      // Validating if the supplied date string is valid and not a NaN (Not a Number) 
      if (!isNaN(new Date(getYear(date, getYearIndex(format)), 
        getMonth(date, getMonthIndex(format)) - 1, 
        getDate(date, getDateIndex(format))))) {      
       return true; 
      } 
      return false;          
     } 
    </script> 

HTML日付関連する入力フィールド

<input type="text" name="start" id="start" size="10" value="" /> 
<br/> 
<input type="text" name="end" id="end" size="10" value="" /> 
<br/> 
<input type="button" value="Submit" onclick="javascript:validate();" />