Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check Date

Status
Not open for further replies.

toddsalcedo

Programmer
Jul 2, 2001
49
US
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>
 

I've not tried this, but it should work:

Code:
var now = new Date();
var nowDate = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate(), 15, 0, 0, 0);
var yourDate = new Date(theYear, theMonth - 1, theDate, 15, 0, 0, 0);

if (nowDate.getTime() < yourDate.getTime()) alert('Your date is in the future!');

Hope this helps,
Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Thank you, I haven't had much of a chance to mess with it, but I'll give it a shot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top