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.

Attachments
<?php
define("EW_PAGE_ID", "add", TRUE); // Page ID
define("EW_TABLE_NAME", 'campaign_info_table', TRUE);
?>
<?php 
session_start(); // Initialize session data
ob_start(); // Turn on output buffering
?>
<?php include "ewcfg50.php" ?>
<?php include "ewmysql50.php" ?>
<?php include "phpfn50.php" ?>
<?php include "campaign_info_tableinfo.php" ?>
<?php include "userfn50.php" ?>
<?php include "login_tableinfo.php" ?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php

// Open connection to the database
$conn = ew_Connect();
?>
<?php
$Security = new cAdvancedSecurity();
?>
<?php
$userlevelid = $Security->getCurrentUserLevelID();
if (!$Security->IsLoggedIn()) $Security->AutoLogin();
$Security->LoadCurrentUserLevel('campaign_info_table');
if (!$Security->IsLoggedIn()) {
	$Security->SaveLastUrl();
	Page_Terminate("login.php");
}
if (!$Security->CanAdd()) {
	$Security->SaveLastUrl();
	Page_Terminate("campaign_info_tablelist.php");
}
if ($Security->IsLoggedIn() && $Security->CurrentUserID() == "") {
	$_SESSION[EW_SESSION_MESSAGE] = "You do not have the right permission to view the page";
	Page_Terminate("login.php");
}
?>
<?php

// Common page loading event (in userfn*.php)
Page_Loading();
?>
<?php

// Page load event, used in current page
Page_Load();
?>
<?php
$campaign_info_table->Export = @$_GET["export"]; // Get export parameter
$sExport = $campaign_info_table->Export; // Get export parameter, used in header
$sExportFile = $campaign_info_table->TableVar; // Get export file, used in header
?>
<?php

// Load key values from QueryString
$bCopy = TRUE;
if (@$_GET["campaign_id"] != "") {
  $campaign_info_table->campaign_id->setQueryStringValue($_GET["campaign_id"]);
} else {
  $bCopy = FALSE;
}

// Create form object
$objForm = new cFormObj();

// Process form if post back
if (@$_POST["a_add"] <> "") {
  $campaign_info_table->CurrentAction = $_POST["a_add"]; // Get form action
  LoadFormValues(); // Load form values
} else { // Not post back
  if ($bCopy) {
    $campaign_info_table->CurrentAction = "C"; // Copy Record
  } else {
    $campaign_info_table->CurrentAction = "I"; // Display Blank Record
    LoadDefaultValues(); // Load default values
  }
}

// Perform action based on action code
switch ($campaign_info_table->CurrentAction) {
  case "I": // Blank record, no action required
		break;
  case "C": // Copy an existing record
   if (!LoadRow()) { // Load record based on key
      $_SESSION[EW_SESSION_MESSAGE] = "No records found"; // No record found
      Page_Terminate($campaign_info_table->getReturnUrl()); // Clean up and return
    }
		break;
  case "A": // ' Add new record
		$campaign_info_table->SendEmail = TRUE; // Send email on add success
    if (AddRow()) { // Add successful
      $_SESSION[EW_SESSION_MESSAGE] = "Add New Record Successful"; // Set up success message
      Page_Terminate($campaign_info_table->KeyUrl($campaign_info_table->getReturnUrl())); // Clean up and return
    } else {
      RestoreFormValues(); // Add failed, restore form values
    }
}

// Render row based on row type
$campaign_info_table->RowType = EW_ROWTYPE_ADD;  // Render add type
RenderRow();
?>
<?php include "header.php" ?>
<script type="text/javascript">
<!--
var EW_PAGE_ID = "add"; // Page id

//-->
</script>
<script type="text/javascript">
<!--
function ew_ValidateForm(fobj, limit) {
	if (fobj.a_confirm && fobj.a_confirm.value == "F")
		return true;
	var i, elm, aelm, infix,esd,eed;
	var rowcnt = (fobj.key_count) ? Number(fobj.key_count.value) : 1;
	

	for (i=0; i<rowcnt; i++) {
		infix = (fobj.key_count) ? String(i+1) : "";
		elm = fobj.elements["x" + infix + "_campaign_id"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - Campaign ID"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_campaign_id"];
		if (elm && !ew_CheckInteger(elm.value)) {
			if (!ew_OnError(elm, "Incorrect integer - Campaign ID"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_campaign_name"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - Campaign Name"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_campaign_start_date"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - Campaign Start Date"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_campaign_start_date"];
		if (elm && !ew_CheckDate(elm.value)) {
			if (!ew_OnError(elm, "Incorrect date, format = yyyy/mm/dd - Campaign Start Date"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_campaign_end_date"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - Campaign End Date"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_campaign_end_date"];
		if (elm && !ew_CheckDate(elm.value)) {
			if (!ew_OnError(elm, "Incorrect date, format = yyyy/mm/dd - Campaign End Date"))
				return false; 
		}
		esd = fobj.elements["x" + infix + "_campaign_start_date"];
		eed = fobj.elements["x" + infix + "_campaign_end_date"];
		if (Date.parse(esd)>Date.parse(eed)) {
			if (!ew_OnError(eed, "Campaign End Date should be after campaign start date"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_zone[]"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - Zone"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_schedule_options"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - Schedule Options"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_start_time"];
		if (elm && !ew_CheckTime(elm.value)) {
			if (!ew_OnError(elm, "Incorrect time (hh:mm:ss) - Start Time"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_hours_valid"];
		if (elm && !ew_CheckInteger(elm.value)) {
			if (!ew_OnError(elm, "Incorrect integer - Hours Valid"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_StatusFlag"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - Status Flag"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_user_id"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - User ID"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_user_id"];
		if (elm && !ew_CheckInteger(elm.value)) {
			if (!ew_OnError(elm, "Incorrect integer - User ID"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_voting_options[]"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - Voting Options"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_ivr_reponse_options"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - IVR Reponse Options"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_sms_reponse"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - SMS Reponse Options"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_ivr_num"];
		if (elm && !ew_CheckInteger(elm.value)) {
			if (!ew_OnError(elm, "Incorrect integer - IVR Number"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_sms_short_code"];
		if (elm && !ew_CheckInteger(elm.value)) {
			if (!ew_OnError(elm, "Incorrect integer - SMS Short Code"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_multiple_votes_allowed"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - Multiple Votes Allowed"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_sms_charge"];
		if (elm && !ew_CheckInteger(elm.value)) {
			if (!ew_OnError(elm, "Incorrect integer - SMS Charge"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_ivr_charge"];
		if (elm && !ew_CheckInteger(elm.value)) {
			if (!ew_OnError(elm, "Incorrect integer - IVR Charge"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_ivr_success_id"];
		if (elm && !ew_CheckInteger(elm.value)) {
			if (!ew_OnError(elm, "Incorrect integer - IVR Success ID"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_ivr_invalid_error_id"];
		if (elm && !ew_CheckInteger(elm.value)) {
			if (!ew_OnError(elm, "Incorrect integer - IVR Invalid Error ID"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_ivr_multiple_vote_id"];
		if (elm && !ew_CheckInteger(elm.value)) {
			if (!ew_OnError(elm, "Incorrect integer - IVR Multiple Vote ID"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_ivr_wrong_contestent_id"];
		if (elm && !ew_CheckInteger(elm.value)) {
			if (!ew_OnError(elm, "Incorrect integer - IVR Wrong Contestent ID"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_ivr_campaign_not_active_id"];
		if (elm && !ew_CheckInteger(elm.value)) {
			if (!ew_OnError(elm, "Incorrect integer - IVR Campaign Not Active ID"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_contestent_count"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - Contestent Count"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_contestent1Id"];
		if (elm && !ew_CheckInteger(elm.value)) {
			if (!ew_OnError(elm, "Incorrect integer - Contestent 1 ID"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_contestent1IvrCode"];
		if (elm && !ew_CheckInteger(elm.value)) {
			if (!ew_OnError(elm, "Incorrect integer - Contestent 1 IVR Code"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_contestent2Id"];
		if (elm && !ew_CheckInteger(elm.value)) {
			if (!ew_OnError(elm, "Incorrect integer - Contestent 2 ID"))
				ret

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

This question has already been answered. Start a new discussion instead.