1

I want to validate Time-Start and Time-End. The time format is like this 2:15:00 AM I want to make sure that the Time-End must be greater or equal to the Time-Start.

The Time-Start gets the time from DropDownListHourStart(1-12) and DropDownListMinuteStart(1-59) and DropDownListSecondStart(00) and DropDownListAMPMStart(AM-PM)

The Time-End gets the time from DropDownListHourEnd(1-12) and DropDownListMinuteEnd(1-59) and DropDownListSecondEnd(00) and DropDownListAMPMEnd(AM-PM)

I want to check that time-end is equal to or greater than time-in. Can you show me any time validation technique? Whether it's jQuery, JavaScript, or ASP.NET control validator. Thank you.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Loso
  • 7
  • 1
  • 2
  • http://stackoverflow.com/questions/497294/how-can-i-use-comparevalidator-for-times-without-dates – jjj Jan 31 '10 at 08:48
  • you can compar it as string in JavaScript ..without comparevalidator – jjj Jan 31 '10 at 08:51

2 Answers2

0

Assuming the same date:

function timeToSec(str)
{
  var h = Number(str.match(/^\d+/));
  if(str.indexOf("PM") != -1)
    h += 12;
  var m = Number(str.match(/^\d+:(\d+):/)[1]);
  var s = Number(str.match(/:(\d+)\s+[AP]M/)[1]);
  return (h * 60 + m) * 60 + s;
}
if(timeToSec(start) > timeToSec(end))
  alert("Oops");
Amarghosh
  • 58,710
  • 11
  • 92
  • 121
0
<script type="text/javascript" >
        var j = document.getElementById('DropDownListHourStart').value+":"+document.getElementById('DropDownListMinuteStart').value+":"+document.getElementById('DropDownListSecondStart').value+" "+document.getElementById('DropDownListAMPMStart').value;
        var d = document.getElementById('DropDownListHourEnd').value+":"+document.getElementById('DropDownListMinuteEnd').value+":"+document.getElementById('DropDownListSecondEnd').value+" "+document.getElementById('DropDownListAMPMEnd').value;

        if (d >= j) {
            document.getElementById('your_label_id').innerText = "the Time-End must be greater or equal to the Time-Start";
        }
        </script>
jjj
  • 605
  • 1
  • 9
  • 26