2010-01-31 86 views
1

我想验证时间开始和时间结束。时间格式如此2:15:00 AM我想确保时间结束必须大于或等于时间开始。时间比较验证器

时间开始会从DropDownListHourStart(1-12)和DropDownListMinuteStart(1-59)和DropDownListSecondStart(00)和DropDownListAMPMStart(AM-PM)

时间结束从DropDownListHourEnd获取时间的时间(1-12)和DropDownListMinuteEnd(1-59)和DropDownListSecondEnd(00)和DropDownListAMPMEnd(AM-PM)

我想检查时间结束是否等于或大于时间。你能告诉我任何时间验证技术吗?无论是jQuery,JavaScript还是ASP.NET控件验证器。谢谢。

+0

http://stackoverflow.com/questions/497294/how-can-i-use-comparevalidator-for-times-without-dates – jjj 2010-01-31 08:48:47

+0

可以在JavaScript中比较它作为字符串..没有comparevalidator – jjj 2010-01-31 08:51:06

回答

0

假设同一日期:

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"); 
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>