...

View Full Version : Validating date in seperate fields on form?



TronixGT
05-24-2009, 04:14 PM
I see 10,000 ways to validate date fields on forms. But all of them are based on a single text box using mm/dd/yyyy type formats. My forms have the date seperated into individual fields. a 2 digit month in one input box, 2 digit day input box and 4 digit year input box.

month.text
day.text
year.text

I'm using asp.net forms so using range validator I validate that the month field is between 1-12 day is between 1-31 and year is between 1800-2250, but that really doesnt do much good. Users can still enter invalid dates such as: 02/30/2010.

I also have a seperated date field that must be either "today" or a future date.

There are scripts everywhere for these if we used the old standard single text box, but since mine are seperate fields how do I do this?

Philip M
05-24-2009, 05:23 PM
Read in the values of the three fields into this script:-



<script type = "text/javascript">

function checkValidDate() {
var yr = 2009;
var mm = 1; // mm = 0 - 11 so 1 is February
var dd = 31
var nd = new Date();
nd.setFullYear(yr,mm,dd); // YYYY,MM(0-11),DD
var ndmm = nd.getMonth();
if (ndmm != mm) {
alert (dd + "/" + mm + "/" + yr + " is an Invalid Date!");
return false;
}
else {
alert (dd + "/" + mm + "/" + yr + " is a Valid Date");
}
}

checkValidDate()
</script>

Likewise:-


<script type = "text/javascript">

function checkFutureDate() {
var end_year = 2009;
var end_month = 4; // 0 - 11 so 4 is May
var end_day = 2;

var now = new Date().getTime();
var d = new Date();
d.setFullYear(end_year, end_month, end_day); // YYYY,MM(0-11),DD
var selectedDate = d.getTime();
if (selectedDate <= now) { // after today's date
// if (selectedDate < now) { // including today's date or after
alert ("Date must be after today's date!");
return false;
}
alert ("Date is valid");
return true;
}

checkFutureDate()
</script>



All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

TronixGT
05-24-2009, 08:26 PM
Thanks Philip, That at least got me started in the right direction.

The first script basically accomplished the same thing that RangeValidation in asp.net does. it made sure month is 1-12 day is 1-31.. etc. But it did not account for days like february 29th or february 30th. I have found some code that does work, and does check for a true valid date. That seems to be working now.

I am trying to use the FutureDate function you posted, but it is allowing any date in the month of may and it should only allow today and future dates.. so it doesnt quite check the month/day right. Is this a MM/DD/YYYY type format issue? I can't figure this out.

Here is what I have..

Thanks in advance!



<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
function isDate(year, month, day) {
month = month - 1; // javascript month range 0 - 11
var tempDate = new Date(year,month,day);
if ( (year == tempDate.getFullYear()) &&
(month == tempDate.getMonth()) &&
(day == tempDate.getDate()) ) {
return true;
} else {
return false;
}
}
function checkValidDate(form) {
with (form) {
var monthValue = document.getElementById('month').value;
var dayValue = document.getElementById('day').value;
var yearValue = document.getElementById('year').value;
if (!isDate(yearValue,monthValue,dayValue)) {
alert("This is not a valid date!");
return false;
}
}
alert ("Date is valid date");
return true;
}
function checkFutureDate() {
var end_month = document.getElementById('month').value; // 0 - 11 so 4 is May
var end_day = document.getElementById('day').value;
var end_year = document.getElementById('year').value;

var now = new Date().getTime();
var d = new Date();
d.setFullYear(end_year, end_month, end_day); // YYYY,MM(0-11),DD
var selectedDate = d.getTime();
if (selectedDate <= now) { // after today's date
// if (selectedDate < now) { // including today's date or after
alert ("Date must be today or after today's date!");
return false;
}
checkValidDate(this);
return true;
}
</script>


</HEAD>
<BODY>
<FORM ONSUBMIT="checkFutureDate()">

<INPUT TYPE="text" NAME="month" SIZE="2" MAXLENGTH="2">
<INPUT TYPE="text" NAME="day" SIZE="2" MAXLENGTH="2">
<INPUT TYPE="text" NAME="year" SIZE="4" MAXLENGTH="4">
<INPUT TYPE="submit" VALUE="Validate">
</FORM>
</BODY>
</HTML>

Philip M
05-25-2009, 08:45 AM
The scripts I offered you work fine for me.

checkValidDate(this);
return true;

will always return true. Delete the line in blue.

The reason it pases any date in May is that you have not taken on board that months in Javascript are 0-11, so May is month 4. Month 5 is June, and any date in June is ahead of today.

There is nothing in your scripts to validate that the values entered are numbers.

<SCRIPT LANGUAGE="JavaScript"> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum