Here is my current date validation function:
isValidDate = function(day,month,year) {
var dteDate;
dteDate=new Date(year,month,day);
return ((day.toString()===dteDate.getDate().toString()) && (month.toString()===dteDate.getMonth().toString()) && (year.toString()===dteDate.getFullYear().toString()));
}
Then later I check the fields:
checkFields = function() {
var iDate = $("inspect_date").value;
if(iDate.length > 0) {
var a = iDate.split("/");
if(isValidDate(a[0],a[1]-1,a[2]) == false){
alert("You have entered an invalid date. Please amend!");
return false;
}
So at the moment it doesn't accept dates in the format dd/mm/yyy which is what I want - the function doesn't like the leading zero.
I tried to fix it in this way:
isValidDate = function(day,month,year) {
var dteDate;
dteDate=new Date(year,month,day);
var day = dteDate.getDate();
var month = dteDate.getMonth() + 1;
var year = dteDate.getFullYear();
var formatted =
(day < 10 ? "0" : "") + day + "/" +
(month < 10 ? "0" : "") + month + "/" +
year;
return ((day.toString()===dteDate.getDate().toString()) && (month.toString()===dteDate.getMonth().toString()) && (year.toString()===dteDate.getFullYear().toString()));
}
But now my 'return' part is containing the wrong values when it does the comparisons.
Can anyone help?