Google Answers Logo
View Question
 
Q: Javascript - Month and Year Drop Down Fields - Cannot Select date in the Past ( No Answer,   5 Comments )
Question  
Subject: Javascript - Month and Year Drop Down Fields - Cannot Select date in the Past
Category: Computers > Programming
Asked by: nyoung-ga
List Price: $10.00
Posted: 12 Oct 2004 12:58 PDT
Expires: 11 Nov 2004 11:58 PST
Question ID: 413829
I am looking for Javascript that will stop a customer selecting a
month and year in the past.  I would prefer to have the error message
appear when the year and month are selected and not on form submit.

My form is called "form1" and the two dropdown fields are named "month" and "year".

Thanks

Request for Question Clarification by palitoy-ga on 13 Oct 2004 01:21 PDT
Hello nyoung-ga

Can you please post the script that you already have?  Do you require
a pop-up calendar type solution or a solution using drop down boxes
(as joshfraz-ga is indicating below)?

If you require a pop-up calendar, would directing you to a free
solution on the internet be sufficient for an answer?  I regularly use
one javascript calendar that I know does exactly what you require,
this calendar does not allow dates in the past to be picked and
crosses them out on the display as the day changes.  It is a very nice
looking solution!

Clarification of Question by nyoung-ga on 13 Oct 2004 04:25 PDT
Hi, the way Josh has answered my query is spot on, I just can't seem
to get it to work on my page or on a blank page.

Thanks

Nicola
Answer  
There is no answer at this time.

Comments  
Subject: Re: Javascript - Month and Year Drop Down Fields - Cannot Select date in the Past
From: joshfraz-ga on 12 Oct 2004 19:17 PDT
 
Hey there,

I believe this is the function that you are needing:  

<script language=javascript>

function testDate() {
	var date=new Date();
	var month = date.getMonth();
	var year = date.getYear();

if (form_year < year || (year == document.form1.month.value &&
document.form1.year.value < month))
	window.alert("Oops!  Please make sure that you enter a future date.");
}

</script>

I tested it using this form:

<form name=form1>
<select name="month">
<option>Month</option>
<option value=0>Jan</option>
<option value=1>Feb</option>
<option value=2>Mar</option>
<option value=3>Apr</option>
<option value=4>May</option>
<option value=5>Jun</option>
<option value=6>Jul</option>
<option value=7>Aug</option>
<option value=8>Sep</option>
<option value=9>Oct</option>
<option value=10>Nov</option>
<option value=11>Dec</option>
</select>

<select name="year" onchange=testDate()>
<option>Year</option>
<option value=2003>2003</option>
<option value=2004>2004</option>
<option value=2005>2005</option>
</select>
</form>

Notice that you can modify the month dropdown to be "January" or "01"
instead of "Jan", but if you do make sure you leave the value
parameter the same.  This script works by comparing the form values to
the current month and year.  The function "testDate()" is called when
you change a value in the date dropdown so be sure to add this
function to your onchange parameter of your year field.

I hope this helps.  Please let me know if I can be of any further assistance.

Josh Fraser
Subject: Re: Javascript - Month and Year Drop Down Fields - Cannot Select date in the Past
From: nyoung-ga on 13 Oct 2004 00:47 PDT
 
Hi Josh, thanks very much for your quick reponse, however I cannot get
it to work on my form.

I have also taken exactly what you have written and pasted it straight
into a blank page and still I have had no joy.

I have pasted the script into the head and the form into the body.

Thanks
Subject: Re: Javascript - Month and Year Drop Down Fields - Cannot Select date in the Past
From: paul_schleifer-ga on 13 Oct 2004 04:45 PDT
 
Thy this...



<script type="text/javascript">
function testDate()
	{
	var d = new Date();
	var dm = d.getMonth();
	var dy = d.getYear();
	var fm = parseInt (document.form1.month.value);
	var fy = parseInt (document.form1.year.value);
	if (fy == -1 || fm == -1) return false;
	else if (fy < dy || (dy == fy && fm < dm))
		{
		alert ("Please enter a future or current date");
		return false;
		}
	else return true;
	}
</script>

<form name="form1" onsubmit="return testDate()">
	<select name="month" onchange="return testDate()">
		<option value="-1">Month</option>
		<option value="0">Jan</option>
		<option value="1">Feb</option>
		<option value="2">Mar</option>
		<option value="3">Apr</option>
		<option value="4">May</option>
		<option value="5">Jun</option>
		<option value="62">Jul</option>
		<option value="7">Aug</option>
		<option value="8">Sep</option>
		<option value="9">Oct</option>
		<option value="10">Nov</option>
		<option value="11">Dec</option>
	</select>

	<select name="year" onchange="testDate()">
		<option value="-1">Year</option>
		<option value="2003">2003</option>
		<option value="2004">2004</option>
		<option value="2005">2005</option>
	</select>

	<input type="Submit">
</form>
Subject: Re: Javascript - Month and Year Drop Down Fields - Cannot Select date in the Past
From: joshfraz-ga on 13 Oct 2004 08:18 PDT
 
Hey there,

I am sorry that you were having difficulty using the code that I
provided.  I would like to thank paul_schleifer-ga for his
improvements to my original code.  Great work!  I would recommend
using the improved version that he supplied.  Please let me know if I
can be of further assistance.

Josh Fraser
Subject: Re: Javascript - Month and Year Drop Down Fields - Cannot Select date in the Past
From: nyoung-ga on 13 Oct 2004 08:28 PDT
 
Thanks, I now have it working with Paul's code.

Thanks everyone

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy