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 DropDownL开发者_JAVA技巧istMinuteStart(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.
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");
<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>
精彩评论