Avatar of ravi_raj123
ravi_raj123
Flag for United Arab Emirates asked on

Compare two date - text boxes

Hi,

In my form I have checkin and checkout text boxes, user enters the dates in dd/mm/yyyy format, how do I compare these two dates? I have to give alert if the checkout date is before the checkin date. Please help.

Cheers :)
JavaScript

Avatar of undefined
Last Comment
ravi_raj123

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
ldbkutty

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
archrajan

credit zvonko
<html>
<head>
<script>
function checkForm(theForm){
  fDate = new Date(theForm.fromDate.value);
  if(isNaN(fDate)){
    alert("Please eneter FromDate.\nFormat: MM/DD/YYYY");
    theForm.fromDate.focus();
    return false;
  }
  if((fDate.getTime()+86400000) < (new Date())){
    alert("FromDate is in the past.");
    theForm.fromDate.select();
    theForm.fromDate.focus();
    return false;
  }
  tDate = new Date(theForm.toDate.value);
  if(isNaN(tDate)){
    alert("Please eneter ToDate.\nFormat: MM/DD/YYYY");
    theForm.toDate.focus();
    return false;
  }
  if(tDate < fDate){
    alert("FromDate is greater the ToDate.");
    theForm.fromDate.select();
    theForm.fromDate.focus();
    return false;
  }
  return true;
}
</script>
</head>
<body>
<form onSubmit="return checkForm(this)">
From:
<input type=text name="fromDate">
To:
<input type=text name="toDate">
<input type=submit>
</form>
</body>
</html>
archrajan

oops the one i posted checks and validates against todays date which is not ur requirement.. ignore it pls..
ravi_raj123

ASKER
Idbkutty thanks for your solution. I did exactly what you answered but before you answered. But I will give accept your answer.

Archrajan thanks for your effort :)
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck