Code:
function validDate() {
var re= /^\d{2}[.\/-]\d{2[.\/-]\d{4}$/;
if (re.test(addDueDate)== false) {
alert("please enter a valid date");
}
}
That checks if the format of the date is correct, (but note that USA and UK formats differ), but does not at all check whether the date is valid - not 56th February, or 26th of month 34, or year 9235. So not very useful.
Code:
<script type = "text/javascript">
function checkValidDate(yr,mmx,dd) {
if (yr <1910 || yr >2012) { // you may want to change 2012 to some other year!
alert ("Year is out of range")
return false;
}
mm = mmx-1; // remember that in Javascript date objects the months are 0-11
var nd = new Date(yr,mm,dd);
var ndmm = nd.getMonth();
if (ndmm != mm) {
alert (dd + "/" + mmx + "/" + yr + " is an Invalid Date!");
return false;
}
else {
alert (dd + "/" + mmx + "/" + yr + " is a Valid Date");
}
}
// USAGE:
checkValidDate(2012,2,20) // 20th February 2012 yyyy/mm/dd
checkValidDate(2012,2,31) // 31st February 2012 yyyy/mm/dd
</script>
As felgall says, alerts are used only for debugging/demonstration.