0

I have a field HrsBirthDate which should take input in format HH:MM (it can be 00:01, 01:19, 23:44, 19:34, 13:12).

So before submitting the form I need to check whether the value is in any of the above formats else throw an error message.

Now only I can check that the field is not empty.

This is my code:

        $("body").on("click", "#WebGrid TBODY .Update", function () {
           var row = $(this).closest("tr");
           var employee = {};
           employee.EmployeeID = row.find(".EmployeeId").find(".label").html();
           employee.FirstName = row.find(".Name").find(".text").val();
           employee.BirthDate = row.find(".DOB").find(".text").val();
           employee.HrsBirthDate = row.find(".HrsBirthDate").find(".text").val();
 
           if (employee.FirstName != '' && employee.BirthDate != '' && employee.HrsBirthDate != '') {
               debugger;
               $("td", row).each(function () {
                   if ($(this).find(".text").length > 0) {
                       var span = $(this).find(".label");
                       var input = $(this).find(".text");
                       span.html(input.val());
                       span.show();
                       input.hide();
                   }
               });
               row.find(".Edit").show();
               row.find(".Cancel").hide();
               $(this).hide();
               $.ajax({
                   type: "POST",
                   url: "/Home/UpdateEmployee",
                   data: '{employee:' + JSON.stringify(employee) + '}',
                   contentType: "application/json; charset=utf-8",
                   dataType: "json"
               });
           }
           else {
               alert("Please fill the details.");
               return false;
           }
       });

1 Answers1

1

To check whether a string matches a 24h format (hh:mm range 00:00 → 23:59) we can use a small regular expression with RegExp.prototype.test()

const validHHMMstring = (str) => /^([01]?[0-9]|2[0-3]):[0-5][0-9]$/.test(str);

console.log(validHHMMstring("99:99")); // false
console.log(validHHMMstring("24:00")); // false
console.log(validHHMMstring("23:60")); // false
console.log(validHHMMstring("23:59")); // true
console.log(validHHMMstring("00:00")); // true

and use it like:

if (validHHMMstring(employee.HrsBirthDate)) {
  // do something, is valid HH:MM
}
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
  • How could you think this is *not* a duplicate many times over? – Heretic Monkey Jan 12 '21 at 16:43
  • @HereticMonkey the only dupe I could quickly find was [this dupe](https://stackoverflow.com/a/14472703/383904) - and the accepted answer is not even correct. Neither is correct the answer in [this dupe](https://stackoverflow.com/a/5563222/383904) – Roko C. Buljan Jan 12 '21 at 16:47
  • Then add a correct answer to the duplicate of your choice, and try to convince the asker that yours is correct. Or not, a duplicate's accepted answer does not need to be correct to be a duplicate target. – Heretic Monkey Jan 12 '21 at 16:52