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;
}
}
使用String.substr(开始,长度)是有效的 – David 2010-02-08 18:19:35
@Upper Stage:根据w3schools(在我的回答中链接),“所有主流浏览器都支持substr()方法。”我希望看到不支持substr()的浏览器。 – Ricket 2010-02-08 18:22:53
你是对的!它有两个参数。愚蠢的Dreamweaver和它的代码提示。 – JoeBob 2010-02-08 18:23:33