0

I have two text boxes, one is "From Date" and "To Date". user will enter the date in the format of "mm/dd/yyyy". Here the "To Date" is always Greater than "From Date". if not, i will alert the user "Not valide, To date is always greater than From Date".

Ex: From Date: 06/05/2011 To Date: 05/08/2011

The above statement is wrong.

       Please give your answer.
user869143
  • 11
  • 4

2 Answers2

1

First, you'll probably want to use <input type="date">, which does some validation on browsers that support it, and shows as a regular input box on browsers that don't.

For actually validating that one date is before the other, you can use a JavaScript Date Object.

var fromDate = new Date(from.value);
if (isNaN(fromDate.getTime())
  alert("Invalid From Date");
var toDate = new Date(end.value);
if (isNaN(toDate.getTime())
  alert("Invalid To Date");
if (toDate < fromDate)
  alert("Not valid, To date is always greater than From Date");
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Annie
  • 6,621
  • 22
  • 27
0

That's one way to do it. Another might be to ask if the user wishes to reverse the two.

duffymo
  • 305,152
  • 44
  • 369
  • 561