最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

validation - Simple JavaScript not executing? - Stack Overflow

programmeradmin1浏览0评论

What could be the cause of the validateDate() function not to execute when called?

The purpose of validateDate() is to take a string like 01/01/2001 and call isValidDate() to determine if the date is valid.

If it's not valid, then a warning message will appear.

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

What could be the cause of the validateDate() function not to execute when called?

The purpose of validateDate() is to take a string like 01/01/2001 and call isValidDate() to determine if the date is valid.

If it's not valid, then a warning message will appear.

 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 paring 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;
     }
 }
Share Improve this question asked Feb 8, 2010 at 18:11 JoeBobJoeBob 1131 gold badge2 silver badges5 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

substr is not a function by itself; you must use string.substr(start_index, length).

Since the JavaScript substr method only takes two parameters, not three, this causes execution to halt at the first substr line, and you'll never get output from that function.

I found this by opening Firebug when running your code in a test HTML page. I highly remend using Firebug for JavaScript debugging.

Try this in your validateDate function, or something similar:

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

substr is not defined... you need

datestring.substr(0, 2);

you also have a problem with your second substring- it should start at character 3:

day   = substr(datestring, 3, 2);

and, month really should be (month - 1) when you create your date

Looking at your code, your date format is "MMDD__YYYY". So your function needs to be as follows:

 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 paring 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 your date is in a more regular format, you can do the following to test if ((new Date("MM/DD/YYYY")) != "Invalid Date")

发布评论

评论列表(0)

  1. 暂无评论