Well, if you can solve the algorithm to check the dates in PHP, you will have no problem with that in javascript either. Just be careful with months in js, as they range from 0-11, not 1-12. Days do go from 1-[max for month], and iirc you can index days out of range just like in PHP, which will autoswitch the month/year for you as necessary. Do double check this to be certain (e.g. 01/00/09 will give you 12/31/08).
Three differenat ways to perform the check. 2 of them prevents submit, 1 of them initiates it.
1. From the form's submit button. validateDate() must return false if the check is NOT ok, and this will prevent the request from being sent to the server.
DO NOTE: if you just use onclick="return validateDate()", the form will always send. Don't forget that ;
<input type="submit" onclick="return validateDate();" value="Send"/>
From form event. Works as the above
<form action="" method="post" onsubmit="return validateDate();" />
Using a button instead of a submit. No need for a return here. If the check is ok in validateDate(), you will end it with f.submit(), where f is your form.
<input type="button" onclick="validateDate();" value="Send"/>
As for the javascripting, you will need
// not actually needed, but it saves some typing...
var d = document;
/* Argument should match id for an element as specified in the html
code: <input type="text" id="fromDate" /> */
var object = d.getElementById('fromDate')
// In case there was no such control, object is undefined (or perhaps null)
if (object)
/* If you roll with validation option 3, you can either get the form object
with d.getElementById(), or... */
d.forms[0] // ... provided it is the first form on that page
The best javascript reference I've found, is the Mozilla js reference.
On that page you have several links. The two most important are:
- Core Javascript Reference, for all basic js functionality: arrays, strings, date, for ... in, if ... else etc. The links 1.6 to 1.9 are not quite as valuable, since a lot of that stuff won't work in other browsers (well, not in IE anyway. Safari 4 might handle it)
- Gecko DOM Reference, for all DOM (Document Objet Model) related stuff, i.e. ways of interacting with elements on the page. While Gecko is the Firefox javscript engine, they sometimes include information on how to handle things in other browsers as well, e.g. how to add event listeners (addEventListener vs attachEvent)