You are viewing limited content. For full access, please sign in.

Question

Question

Time validation on two fields

asked on February 4, 2016

I am looking for a way to validate user-entered time fields by comparing the closing time to opening time.  The opening time CANNOT be greater than the closing time.  

I am able to get an entry level validation, but would like to include military time and/or AM/PM validation to this.  

Below is a screenshot of my form:

Below is the code I'm using:

//Start time validation
  $('.masterClose').on('blur','input', function() {
    var startTime = $('.masterOpen input').val();
    var endTime = $('.masterClose input').val();
    var msgText = ("Closing time must be after " + startTime + ".  Please update time(s)");
    if (startTime > endTime) {
    alert(msgText);
  }
});
//End time validation

This code is working for time less than the entered value.  This code does NOT take into account AM/PM

Example seen above (03:00 pm and 02:59 pm) throws the correct alert.  However, if I enter 03:00pm and 10:00am, the alert message does not appear.  

Can anyone provide some guidance on how to accommodate AM/PM in this validation?

Thanks,

Nate

0 0

Replies

replied on February 4, 2016

I would recommend using moment.js for this. Then you can use custom javascript like

$(document).ready(function () {
  
  $.getScript('http://server/Forms/js/moment.js');

  $('.masterClose').on('blur','input', function() {
    var startTime = moment($('.masterOpen input').val(), ["h:mm A"]).format("HH:mm");
    var endTime = moment($('.masterClose input').val(), ["h:mm A"]).format("HH:mm");
    var msgText = ("Closing time must be after " + $('.masterOpen input').val() + ".  Please update time(s)");
    if (startTime > endTime) {
      alert(msgText);
    }
  });

});
1 0
replied on February 4, 2016

One solution i could think of is using 24 hour time scale. First convert each time to its respective 24 hour time frame and then comapre. 

0 0
replied on February 4, 2016

Thanks Junaid.  I converted to 24 Hour and this worked.  Does anyone else know a way to accomplish this using 12 hour format?

Thanks,

Nate

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.