...

View Full Version : validating date range



newjs
05-24-2010, 03:37 PM
I want to validate two a date range using javascript, so that my program would check startdate less than end date.

The below function only check the year, and it works.






function validate()
{

var start=document.entry.datum1.value;
year = start.substr(0, 4);
month = start.substr(5, 2);
dayval = start.substr(8, 2);

var end=document.entry.datum2.value;
year1 = end.substr(0, 4);
month1 = end.substr(5, 2);
dayval1 = end.substr(8, 2);


var myDate=new Date();
myDate.setFullYear(year,month,dayval);
var today = new Date();
today.setFullYear(year1,month1,dayval1);

if (myDate> today)
{
alert("The start date must come before the end date");
return false
}

return true;
}
But I have write a function for checking the whole date value and it didnt work.
My date is in format-
2008-10-23(yyyy-mm-dd)


function validate()
{
var start=entry.datum1.value;
year = start.substr(0, 4);
month = start.substr(5, 2);
dayval = start.substr(8, 2);

intyear = parseInt(year, 10);
intmonth = parseInt(month, 10);
intdayval = parseInt(dayval, 10);

var end=entry.datum2.value;
year1 = end.substr(0, 4);
month1 = end.substr(5, 2);
dayval1 = end.substr(8, 2);

intyear1 = parseInt(year1, 10);
intmonth1 = parseInt(month1, 10);
intdayval1 = parseInt(dayval1, 10);

var startDate = Date.parse(intyear,intdayval,intmonth);
var endDate = Date.parse(intyear1,intdayval1,intmonth1);

if (startDate > endDate)
{
alert("The start date must come before the end date");
return false;
}
return true;

}

Please help

Philip M
05-24-2010, 04:05 PM
Try this:-


<script type = "text/javascript">

function checkDate() {
var firstDate = new Date(2009,7,31); // YYYY,MM(0-11),DD
// remember that in Javascript date objects the months are 0-11
var secondDate = new Date(2009,8,1); // should pass
//var secondDate = new Date(2009,7,16); // should fail

if (firstDate > secondDate) {
alert ("Invalid - The first date is after the second date");
return false;
}
else {
alert ("Valid - The second date is after the first date");
return true;
}

}

checkDate();
</script>


If you want to check that the date is a valid date (e.g. not 31st February) you can use:-


<script type = "text/javascript">

function checkValidDate(yr,mmx,dd) {

mm = mmx-1; // remember that in Javascript date objects the months are 0-11
var nd = new Date();
nd.setFullYear(yr,mm,dd); // format YYYY,MM(0-11),DD

var ndmm = nd.getMonth();
if (ndmm != mm) {
alert (dd + "/" + mmx + "/" + yr + " is an Invalid Date!");
return false;
}
else {
alert (dd + "/" + mmx + "/" + yr + " is a Valid Date");
}
}

checkValidDate(2010,2,31) // 31st February 2010
</script>


This is the steepest part of the course and I am afraid that it gets steeper later on. - Sports commentator

newjs
05-24-2010, 04:28 PM
Hello,
thank you for your reply.
But I am accepting the date from a textbox, in the input page.
So I am getting a date in format.


var start=entry.datum1.value;
year = start.substr(0, 4);
month = start.substr(5, 2);
dayval = start.substr(8, 2);
start=2009-09-06
as u have mentioned javascript accept month in format 0-11, then how can I convert myinput month to the required java script month format.

Thank you

Philip M
05-24-2010, 04:40 PM
Hello,
thank you for your reply.
But I am accepting the date from a textbox, in the input page.
So I am getting a date in format.


var start=entry.datum1.value;
year = start.substr(0, 4);
month = start.substr(5, 2);
dayval = start.substr(8, 2);
start=2009-09-06
as u have mentioned javascript accept month in format 0-11, then how can I convert myinput month to the required java script month format.

Thank you

Well, surely you can adapt the date format to suit: e.g.

var start = "2009-09-06"; // 6th September 2009
start = start.replace(/-|:|\s/g,"/"); // allow slashes colons and spaces as well as hyphens as seperators
var s = start.split("/"); // note this accepts 2009-9-6 or 2009/9/6 as well
then yr = s[0]*1, mm = s[1]*1, dd = s[2]*1 (use *1 to change string values to numbers)

and if necessary adjust for Javascript months:-

mmx - mm-1;

newjs
05-24-2010, 04:51 PM
sorry

newjs
05-24-2010, 04:55 PM
Sorry, the code is still not working for month, it only checks year.


var start=document.entry.datum1.value;
var s = start.split("-"); // note this accepts 2009-9-6 as well
yr = s[0], mm = s[1], dd = s[2];
mmx=mm-1;



var end=document.entry.datum2.value;
var s1 = end.split("-");
yr1 = s1[0], mm1 = s1[1], dd1 = s1[2];
mmx1=mm1-1;

var firstDate = new Date(yr,mmx,dd);
var secondDate = new Date(yr1,mmx1,dd1); // should pass




if (firstDate > secondDate) {
alert ("Invalid - The start date must come before the end date");
return false;

Philip M
05-24-2010, 06:02 PM
<form name = "entry">
Start Date <input type = "text" name = "datum1"><br>
End Date <input type = "text" name = "datum2"><br>
<input type = "button" value = "Check The Dates" onclick = "checkDates()">
</form>

<script type = "text/javascript">

function checkDates() {
var start=document.entry.datum1.value;
var s = start.split("-"); // note this accepts 2009-9-6 as well
yr = s[0];
mm = s[1];
dd = s[2];
mmx = mm-1;

var end=document.entry.datum2.value;
var s1 = end.split("-");
yr1 = s1[0];
mm1 = s1[1];
dd1 = s1[2];
mmx1 = mm1-1;

var firstDate = new Date(yr,mmx,dd);
var secondDate = new Date(yr1,mmx1,dd1); // should pass

if (firstDate >= secondDate) {
alert ("Invalid - The start date must come before the end date");
return false;
}
else {
alert ("OK");
}

}

</script>

You need to check at some stage that the dates are valid, as the script will accept silly values like 2010-88-59

newjs
05-25-2010, 12:08 PM
Thank you for your help.
Now its working fine.
I think at this stage I don't have to think for date like 2009-92-22, because I am now accepting the date from a calender.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum