简单的JavaScript没有执行?

时间:2010-02-08 18:11:45

标签: javascript validation date

调用时validateDate()函数不执行的原因是什么?

validateDate()的目的是使用01/01/2001之类的字符串并调用isValidDate()来确定日期是否有效。

如果无效,则会出现警告信息。

 function isValidDate(month, day, year){
    /*
    Purpose: return true if the date is valid, false otherwise

    Arguments: day integer representing day of month
    month integer representing month of year
    year integer representing year

    Variables: dteDate - date object

    */
    var dteDate;

    //set up a Date object based on the day, month and year arguments
    //javascript months start at 0 (0-11 instead of 1-12)
    dteDate = new Date(year, month, day);

    /*
    Javascript Dates are a little too forgiving and will change the date to a reasonable guess if it's invalid. We'll use this to our 
    advantage by creating the date object and then comparing it to the details we put it. If the Date object is different, then it must
    have been an invalid date to start with...
    */

    return ((day == dteDate.getDate()) && (month == dteDate.getMonth()) && (year == dteDate.getFullYear()));
 }

 function validateDate(datestring){

     month = substr(datestring, 0, 2);
     day   = substr(datestring, 2, 2);
     year  = substr(datestring, 6, 4);

     if(isValidDate(month, day, year) == false){
         alert("Sorry, " + datestring + " is not a valid date.\nPlease correct this.");
         return false;
     } else {
         return true;
     }
 }

3 个答案:

答案 0 :(得分:7)

substr本身不是一个功能;你必须使用string.substr(start_index, length)

由于JavaScript substr method只接受两个参数而不是三个参数,这会导致执行停止在第一个子行,并且你永远不会从该函数获得输出。

我在测试HTML页面中运行代码时打开Firebug找到了这个。我强烈建议您使用Firebug进行JavaScript调试。

在您的validateDate函数或类似函数中尝试此操作:

month = datestring.substr(0, 2);
day   = datestring.substr(3, 2);
year  = datestring.substr(6, 4);

答案 1 :(得分:0)

substr未定义...您需要

datestring.substr(0, 2);

你的第二个子字符串也有问题 - 它应该从字符3开始:

day   = substr(datestring, 3, 2);

并且,当你创建日期时,月份应该是(月 - 1)

答案 2 :(得分:0)

查看代码,您的日期格式为“MMDD__YYYY”。所以你的功能需要如下:

 function isValidDate(month, day, year){
    /*
    Purpose: return true if the date is valid, false otherwise

    Arguments: day integer representing day of month
    month integer representing month of year
    year integer representing year

    Variables: dteDate - date object

    */
    var dteDate;

    //set up a Date object based on the day, month and year arguments
    //javascript months start at 0 (0-11 instead of 1-12)
    dteDate = new Date(year, month, day);
    alert(d)

    /*
    Javascript Dates are a little too forgiving and will change the date to a reasonable guess if it's invalid. We'll use this to our 
    advantage by creating the date object and then comparing it to the details we put it. If the Date object is different, then it must
    have been an invalid date to start with...
    */

    return ((day == dteDate.getDate()) && (month == dteDate.getMonth()) && (year == dteDate.getFullYear()));
 }

 function validateDate(datestring){

     month = datestring.substring(0, 2);
     day   = datestring.substring(2, 4);
     year  = datestring.substring(6, 10);
     alert(year)

     if(isValidDate(month, day, year) == false){
         alert("Sorry, " + datestring + " is not a valid date.\nPlease correct this.");
         return false;
     } else {
         return true;
     }
 }
validateDate("0202__2010");

如果您的日期采用更常规的格式,则可以执行以下操作来测试if ((new Date("MM/DD/YYYY")) != "Invalid Date")