2010-02-08 65 views
1

validateDate()函数调用时不能执行的原因是什么?简单的JavaScript没有执行?

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

回答

7

substr本身不是一个函数;您必须使用string.substr(start_index, length)

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

我在测试HTML页面中运行代码时通过打开Firebug发现此问题。我强烈建议使用Firebug进行JavaScript调试。

试试这个在您的validateDate功能,或类似的东西:

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

使用String.substr(开始,长度)是有效的 – David 2010-02-08 18:19:35

+0

@Upper Stage:根据w3schools(在我的回答中链接),“所有主流浏览器都支持substr()方法。”我希望看到不支持substr()的浏览器。 – Ricket 2010-02-08 18:22:53

+0

你是对的!它有两个参数。愚蠢的Dreamweaver和它的代码提示。 – JoeBob 2010-02-08 18:23:33

0

SUBSTR没有定义......你需要

datestring.substr(0, 2); 

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

day = substr(datestring, 3, 2); 

和,月真的应该是(月-1)当你cre吃了你的日期

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")

+0

我敢打赌,而不是使用像MMDD__YYYY这样非常奇怪的东西,substr(2,4)是一个错字,或者更可能是他们忘记了位置2处的'/'。 – David 2010-02-08 18:37:11