Hi,

I am developing a module in PHP where I have to ask user to enter dates in two fields. The catch is that the second date entered should come after the first one in a calendar year. eg- if first date field has value of June 2,2008 then the second field should have the date after June 2,2008. It can't have any date before june 2,2008. To validate this I have to use javascripts but it is the first time I am using javascripts. So please refer me afunction that could compare the two fields and act accordingly

if (Date.parse(fromDate) > Date.parse(toDate)) {
alert("Invalid Date Range!\nStart Date cannot be after End Date!")
return false;

}

or

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<meta name="Content-Style-Type" content="text/css">
<title>Example</title>

<script type="text/javascript">
<!--
function check (f) {
var start = new Date (f.start.value);
var end = new Date (f.end.value);

start.setDate (start.getDate() + 28);
if (end >= start) {alert (); return false}
}
// -->
</script>

<style type="text/css">
<!--
fieldset {padding:1ex; width:10em}
label {display:block; margin:1em 0}
input {display:block}
button {display:block; margin:auto}
-->
</style>

</head>
<body>
<form action="some-script.pl" onsubmit="return check (this)">
<fieldset>
<legend>Dates</legend>
<label>Start Date<input name="start" onchange="this.value = new Date (this.value).toDateString()" type="text"></label>
<label>End Date<input name="end" onchange="this.value = new Date (this.value).toDateString()" type="text"></label>
<button type="submit">Submit</button>
</fieldset>
</form>
</body>
</html>

or simply

function dt()
    {
        var ch=Date.parse("06/19/2008");
       
        var currentdt=new Date;
        if(ch>Date.parse(currentdt))
        {
            alert("Date is greater.");
        }
        else
        {
            alert("Date is less");
        }
    }

always mark thread as solved, when you get solution, as it will help others if they are searching for a solution.

Shanti I would have marked the thread as solved if my problem would have solved but it isn't. I don't know why but the function you mentioned isn't helping me. I am attaching the file in which the validation has to be made. I have made changes in it at line number 166. Please have a look.

Hello..
i have posted another two ways which i have posted...
try to implement with those also...
then tel me where will be the error...

I have tried every solution you have given me, Shanti. It's not working. The program isn't generating any error. I think this has got something to do with the format, the javascript is getting the dates. The format it would be recieving dates would be of sort yyyy/mm/dd. That is why it can't compare the dates. Please tell me some solution so that I could compare them in this format.

I have tried every solution you have given me, Shanti. It's not working. The program isn't generating any error. I think this has got something to do with the format, the javascript is getting the dates. The format it would be recieving dates would be of sort yyyy/mm/dd. That is why it can't compare the dates. Please tell me some solution so that I could compare them in this format.

Helloo,
By this code convert your date to which format you want:

$dp=$_POST['date'];
	$dp1=explode("/",$dp);
	$dp2=$dp1[2]."-".$dp1[0]."-".$dp1[1];

In the last line change format according to your application....

How could I pass this value to the javascripts for validation.

How could I pass this value to the javascripts for validation.

Use this format..

StartDate.setFullYear(StartDateSt);
StartDate.setFullYear(StartDateSt[0],StartDateSt[1],StartDateSt[2]);

The format which i have posted earily is for php...sorry...


Check this manual for reference:
http://www.w3schools.com/js/js_obj_date.asp

Thanx Shanti. The job has been done. There was a error from my side that I was overlooking. Your first solution does the work for me. Thanx again