Click to See Complete Forum and Search --> : Validate date


xenia
05-08-2006, 07:00 AM
Hello everyone,

I have a problem with a javascript function and I would like your help.
I have create a page where the user selects a pick up date and drop off date from a pop up calendar and puts the values in a textfield one for the pickup and one for the drop off.Moreover, the user selects from a drop down menu the pick up time and drop off time.What I want to do is to validate whether the drop off date is greater than the pick up date.I have already written the code and is working fine when the user select dates from the same month.
However, I get error when I put as dates from different months for example a pick up date 08/06/2006 and drop off 07/07/2006.

I will post the code of the script and I would appreciate your help or suggestions


<script type="text/javascript">
function check()
{
var dt1 = Date.parse(document.form1.pickupdate.value+" "+document.form1.pickuphours.value+":00");
var dt2 = Date.parse(document.form1.dropoffdate.value+" "+document.form1.dropoffhours.value+":00");
if(dt1>dt2)
{
alert("Please select a pick up date that is before the drop off date!");
return false
}
}
</script>

Thank you in advance,
Xenia

phpnovice
05-08-2006, 06:38 PM
However, I get error when I put as dates from different months for example a pick up date 08/06/2006 and drop off 07/07/2006.
Don't you think sharing the error message, and which line of code on which the error message occurs, might help those whom might be inclined to take a look at your problem?

Kramy
05-08-2006, 08:45 PM
If your alert box is firing when it shouldn't, you might try showing the values of dt1 and dt2 in an alert box of their own. If you brute-force check a couple dozen combinations after, you might be able to spot a pattern.

Aside from that...can't help without seeing more code or a working example.

My knowledge of the date object is also pretty skimpy. Is it DMY, MDY, or YMD?

xenia
05-09-2006, 03:15 AM
Actually is not that I am getting an error but the function that checks the dates appears when the user selects different months for example:
pick up date:25/05/2006 09:00
dro off date: 07/06/2006 09:00

The function will compare the days(25>07) instead of the whole date (25/05/2006< 07/06/2006).

Here is also my code that I have put the check function.



<form action="price_cars2_test.php" method="post" name="form1" id="form1" onsubmit="check();">
<input name="pickupdate" type="text" size="18" id ="pickupdate" value="pick up date" class="body"/>
<a href="javascript:showCal('Calendar3')"><img src="
datebutton.jpg" alt="dates" border="0" /></a>

<select name="pickuphours" class="body">
<option selected value="pickuphour"> Pick up hour</option>
<option value="08:00">08:00</option>
<option value="09:00">09:00</option>
<option value="10:00">10:00</option>
<option value="11:00">11:00</option>
<option value="12:00">12:00</option>
<option value="13:00">13:00</option>
<option value="14:00">14:00</option>
<option value="15:00">15:00</option>
<option value="16:00">16:00</option>
<option value="17:00">17:00</option>
<option value="18:00">18:00</option>
<option value="19:00">19:00</option>
<option value="20:00">20:00</option>
</select>

<input name="dropoffdate" type="text" size="18" value="drop off date" class="body" id="dropoffdate" />
<input name="date" type="text" size="4" value="days" class="body" id="date" style="position:absolute; left: 28px; top: 355px;"/>
<a href="javascript:showCal('Calendar4')"><img src="
datebutton.jpg" alt="dates" border="0" /></a>

<select name="dropoffhours" class="body" onfocus="calcDays()">
<option selected value="dropoffhour"> Drop off hour</option>
<option value="08:00">08:00</option>
<option value="09:00">09:00</option>
<option value="10:00">10:00</option>
<option value="11:00">11:00</option>
<option value="12:00">12:00</option>
<option value="13:00">13:00</option>
<option value="14:00">14:00</option>
<option value="15:00">15:00</option>
<option value="16:00">16:00</option>
<option value="17:00">17:00</option>
<option value="18:00">18:00</option>
<option value="19:00">19:00</option>
<option value="20:00">20:00</option>
</select>
<input name="submit" type="image" id="button" src="
quotemebutt.jpg" style="position:absolute; left: 142px; top: 354px;" />
</form>


Any suggestions?

Thank you,
Xenia

James Gatka
05-09-2006, 06:38 AM
-----

sridhar_423
05-09-2006, 06:55 AM
try doing like this..

var dt1 = Date(document.form1.pickupdate.value+" "+document.form1.pickuphours.value+":00");
var dt2 = Date(document.form1.dropoffdate.value+" "+document.form1.dropoffhours.value+":00");

if(dt1.getTime()>dt2.getTime()) {
alert("Please select a pick up date that is before the drop off date!");
return false
}

phpnovice
05-09-2006, 07:35 AM
Actually is not that I am getting an error but the function that checks the dates appears ... [to] ... compare the days(25>07) instead of the whole date (25/05/2006< 07/06/2006).
Yes, I've noticed that there appears to be an implementation difference from browser to browser when you compare a Date object itself to another Date object -- as follows:
if(dt1>dt2) {
Some browsers apparently use the equivalent of the getTime() method's output (toSource()) for a comparison while others use the output of the toString() method for the comparison. Thus it is always best to be explicit as to what you wish to compare -- instead of ending up with whatever a particular browser's assigned default method happens to be.

So, the answer given in the previous post is the solution for which you're looking.
I just wanted to add an explanation of "why". ;)

EDIT: Also, keep in mind, when using a slash- or dash-delimited string format for your argument, that the Date.parse() method requires an "mm/dd/yyyy" or "mm-dd-yyyy" format. This can also throw off your comparisons.

xenia
05-09-2006, 11:20 AM
Thank you very much for the explanation.However , I change a little bit the code in order to do the comparison writing the following code:


function check()
{
var s_dt1 = document.form1.pickupdate.value.substr(6,4) + ' ' + document.form1.pickupdate.value.substr(3,2) + ' ' + document.form1.pickupdate.value.substr(0,2) + ' ' + document.form1.pickuphours.value + ':00';
var s_dt2 = document.form1.dropoffdate.value.substr(6,4) + ' ' + document.form1.dropoffdate.value.substr(3,2) + ' ' + document.form1.dropoffdate.value.substr(0,2) + ' ' + document.form1.dropoffhours.value + ':00';


if (s_dt1 > s_dt2)
{
alert('Please select a pick up date that is before the drop off date!');
return false;

}
else
{
return true;
}
}



Form:

<form action="price_test.php" method="post" name="form1" id="form1" onsubmit="check();">


It is working but the problem is when the alert appears, the form is being submited even though has error.

Can you please help me?

Thanks,
Xenia

phpnovice
05-09-2006, 11:37 AM
Change this:

onsubmit="check();"

to this:

onsubmit="return check();"

xenia
05-09-2006, 04:10 PM
Thank you very much now it is ok

phpnovice
05-09-2006, 04:24 PM
You're welcome.

Cheers.