views:

233

answers:

3

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 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;
     }
 }
+5  A: 

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 recommend 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);
Ricket
string.substr(start,length) is valid
David
@Upper Stage: according to w3schools (linked to in my answer), "The substr() method is supported in all major browsers." I'd like to see a browser that doesn't support substr().
Ricket
You're right! It DOES take two parameters. Stupid Dreamweaver and it's code hints.
JoeBob
@Ricket - I was responding to your original, unedited comment (actually supporting you). But I am happy to concur; major, current browsers support substr.
Upper Stage
Well then, thanks! Sorry to confuse with all the editing; it's how I roll. :)
Ricket
A: 

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

David
A: 

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

JONYC
I would bet that rather than using something very strange like MMDD__YYYY, that the substr(2,4) is a typo, or more likely they forgot about the '/' at position 2.
David