时间验证(Javascript)

时间:2011-05-04 17:37:21

标签: javascript validation time

我有一个表单(使用JavaScript),用户应该在其中输入工作开始时间,休息开始时间,休息结束时间和工作结束时间等信息。我有4个文本框用于此目的。输入文本框的时间为hhmm格式(仅限24小时)。

要求:

1. The break times (start and end) must be within the work start and end time.
2. Break start must be before break end time.
3. Users can take up work shifts during the day or even overnight.
4. Work time(excluding breaks) should be less than 8 hours and greater than 4 hours.

所以一些典型的有效条目是:

User1-
Start time: 0900
Break start time:1300
Break end time:1400
End time:1600

User2-
Start time:2200
Break start time:2300
Break end time:2330
End time:0400

一些无效的条目是:

Case1 - Break start before break end
 Start time:2200
 Break start time: 2330
 Break end time: 2300
 End time:0400   

Case 2 -Breaks outside work time

 Start time:2200
 Break start time:1830
 Break end time: 1900
 End time:0400

我在编写需求1,2和&的验证码(JavaScript)时遇到问题。请帮帮我。

这是我到目前为止所得到的。 (请注意:我不能使用DATE对象。)

        var wrkSt = getElement('TB1').value;
        var wrkSt_hr = parseFloat(wrkSt.substr(0,2));
        var wrkSt_mn= parseFloat(wrkEd.substr(2,2));

        var brkSt = getElement('TB2').value;
        var brkSt_hr = parseFloat(brkSt.substr(0,2));
        var brkSt_mn= parseFloat(brkEd.substr(2,2));

        var brkEd = getElement('TB3').value;
        var brkEd_hr = parseFloat(brkSt.substr(0,2));
        var brkEd_mn= parseFloat(brkEd.substr(2,2));

        var wrkEd = getElement('TB4').value;
        var wrkEd_hr = parseFloat(wrkEd.substr(0,2));
        var wrkEd_mn= parseFloat(wrkEd.substr(2,2));

        var msg='';
    if ((wrkSt_hr > wrkEd_hr) || ((wrkSt_hr == wrkEd_hr) && (wrkSt_mn >= wrkEd_mn)) )
    {

        msg+='shift overnight selected';                
    }   

if (wrkEd_hr>12){wrkEd_hr-=12;}
if (wrkSt_hr >12){wrkSt_hr -=12;}
if (brkSt_hr>12){brkSt_hr-=12;}
if (brkEd_hr>12){brkEd_hr-=12;}

var Breakdiff = ((brkEd_hr - brkSt_hr)*60) + (brkEd_mn - brkSt_mn);
var Workdiff_tot = ((wrkEd_hr- wrkSt_hr)*60) + (wrkEd_mn -wrkSt_mn); 
var Shiftdiff = Workdiff_tot - Breakdiff; 

if (Shiftdiff > 480) //8hours = 8*60 = 480 min
 {
  msg+='Time greater than 8 hrs';  
 }
 if (Shiftdiff < 240) //4 hours = 4*60 = 240 min
 {
 msg+='Time less than 4 hrs'; 
 }

请帮我查看休息和工作时间的逻辑。请提前获得任何帮助!

1 个答案:

答案 0 :(得分:0)

function validate(start, breakStart, breakEnd, end) {
  var minutes = [];
  for (var i = 0; i < 4; ++i) {
    var time = arguments[i];
    if (!/^(?:[01]\d|2[0-3])[0-5]\d$/.test(time)) {
      throw new Error("Invalid time " + time);
    }
    var minute = minutes[i] = time.substring(0, 2) * 60 + +time.substring(2);
    if (i && minute < minutes[i - 1]) {  // Take into account times that cross midnight.
      minutes[i] = (minute += 24*60);
    }
    if (i && minute <= minutes[i - 1]) {
      throw new Error("Out of order " + arguments[i - 1] + " and " + time);
    }
  }
  if (minutes[3] - minutes[0] > 8*60) { throw new Error("Shift too long"); }
  if (minutes[3] - minutes[0] > 4*60) { throw new Error("Shift too short"); }
}