JavaScript to validate a date

Saumier

Member
J 2.5.7 F 3.0.6
I've been trying for two days without success to get this to work, with every variation I could think of. I've looked at every WIKI, forum, and google.
I'm merely trying to validate a date entered on a form as mm/dd/yyyy. I cannot figure out why this js does not work.
Code:
// alert(Got to JavaScript');
//var dateStr = $('sky1_checkdate___date1').getValue();
var dateStr = form_32.formElements.get('sky1_checkdate___date1').getValue();

alert('Date is 'dateStr); 
 var DayIndex = 1;
 var MonthIndex = 0;
 var YearIndex = 2;
 
        value = dateStr.replace(/-/g, "/").replace(/\./g, "/"); 
        var SplitValue = dateStr.split("/");
        var OK = true;
        if (!(SplitValue[DayIndex].length == 1 || SplitValue[DayIndex].length == 2)) {
            OK = false;
        }
        if (OK && !(SplitValue[MonthIndex].length == 1 || SplitValue[MonthIndex].length == 2)) {
            OK = false;
        }
        if (OK && SplitValue[YearIndex].length != 4) {
            OK = false;
        }
        if (OK) {
            var Day = parseInt(SplitValue[DayIndex], 10);
            var Month = parseInt(SplitValue[MonthIndex], 10);
            var Year = parseInt(SplitValue[YearIndex], 10);
 
            if (OK = ((Year > 1900) && (Year < new Date().getFullYear()))) {
                if (OK = (Month <= 12 && Month > 0)) {
                    var LeapYear = (((Year % 4) == 0) && ((Year % 100) != 0) || ((Year % 400) == 0));
 
                    if (Month == 2) {
                        OK = LeapYear ? Day <= 29 : Day <= 28;
                    }
                    else {
                        if ((Month == 4) || (Month == 6) || (Month == 9) || (Month == 11)) {
                            OK = (Day > 0 && Day <= 30);
                        }
                        else {
                            OK = (Day > 0 && Day <= 31);
                        }
                    }
                }
            }
        }
        return OK;
    }
 catch (e) {
    return false;
}
If I uncomment the alert('Got to JavaScript') I get that alert. But I do not get the next alert to display the date, so obviously I'm not getting it off the form. I've got this in a simple test list/form where I'm entering date1 and then a text field.
Hate to bother you with this stuff, but I need to get this working.
Thanks.
 
Hate to bother you with this stuff

Hey, you are a Silver supporter. It's not a bother, you pay for it! So don't feel like you have to hammer your head on something for 2 days before asking.

However, I will need to see the page, as it's pretty much impossible to debug JS just looking at it.

My only immediate comment is that:

Code:
alert('Date is 'dateStr);

... is an error, and needs to be:

Code:
alert('Date is ' + dateStr);

Are you familiar with using FireBug for debugging JS, by stepping through the code line by line?

If you want to find me on Skype, we can take a look at your page, and I can show you how to more effectively debug this stuff. if you can't get me on Skype, just post or PM the URL.

-- hugh
 
I sent you a message on Skype with the URL.
I guess I enjoy the "chase of the hunt" trying to find solutions. But after awhile I have to cry uncle. I have learned a lot through the forum(s) and googling for solutions.
Fabrik is the best thing since sliced bread. The biggest problem I see is outdated info on the internet and in the documentation. I really wish I had the time and knowledge to work on the documentation.
I also sent you a PM with backend access if you need it.
 
Hmmm, remind me of your Skype name. Or PM me with the URL. You gave me the backend login in the PM, but not the URL, and I can't find anything in Skype today that looks like it is form you, but I've forgotten your Skype name.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top