toddsalcedo
Programmer
I have some Javascript that checks for the proper date format, but I would also like it to check to see if the date entered is not greater than the current day (today. Any ideas on how I can enhance this to do both at once?
<script LANGUAGE="javascript">
<!-- Begin
function datecheck(x) {
var datex;
var form = document.forms[0];
var months = ["01","02","03","04","05","06","07","08","09","10","11","12"];
var days = ["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"];
var re = /\b(1[0-2]|0?[1-9])[\-\/](0?[1-9]|[12][0-9]|3[01])[\-\/]((19|20)\d{2})/;
var input = form.elements[x].value;
var matchArray = re.exec(input);
if (matchArray) {
var theMonth = months[matchArray[1] - 1] + "/";
var theDate = days[matchArray[2] -1] + "/";
var theYear = matchArray[3];
var dateObj = new Date(matchArray[3],matchArray[1]-1,matchArray[2])
form.elements[x].value = theMonth + theDate + theYear;
} else if (form.elements[x].value =="") {
form.elements[x].value =""
} else {
alert("Invaid Date Format" + "\n" + " " + "\n" + "Valid Formats:" + " " + "\n" + "\n" + "1/2/2001" + "\n" + "01/02/2001" + "\n" + "1-2-2001" + "\n" + "01-02-2001")
form.elements[x].value =""
}
}
// End -->
</script>
<script LANGUAGE="javascript">
<!-- Begin
function datecheck(x) {
var datex;
var form = document.forms[0];
var months = ["01","02","03","04","05","06","07","08","09","10","11","12"];
var days = ["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"];
var re = /\b(1[0-2]|0?[1-9])[\-\/](0?[1-9]|[12][0-9]|3[01])[\-\/]((19|20)\d{2})/;
var input = form.elements[x].value;
var matchArray = re.exec(input);
if (matchArray) {
var theMonth = months[matchArray[1] - 1] + "/";
var theDate = days[matchArray[2] -1] + "/";
var theYear = matchArray[3];
var dateObj = new Date(matchArray[3],matchArray[1]-1,matchArray[2])
form.elements[x].value = theMonth + theDate + theYear;
} else if (form.elements[x].value =="") {
form.elements[x].value =""
} else {
alert("Invaid Date Format" + "\n" + " " + "\n" + "Valid Formats:" + " " + "\n" + "\n" + "1/2/2001" + "\n" + "01/02/2001" + "\n" + "1-2-2001" + "\n" + "01-02-2001")
form.elements[x].value =""
}
}
// End -->
</script>