Link to home
Create AccountLog in
Avatar of ravi_raj123
ravi_raj123Flag 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 :)
ASKER CERTIFIED SOLUTION
Avatar of ldbkutty
ldbkutty
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of archrajan
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>
oops the one i posted checks and validates against todays date which is not ur requirement.. ignore it pls..
Avatar of 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 :)