validation - Simple JavaScript not executing? - Stack Overflow

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745359738a4624303.html

相关推荐

  • validation - Simple JavaScript not executing? - Stack Overflow

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

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信