0

I am trying to validate a time duration user input using jQuery and it keeps returning invalid even though input is valid. Regular expression validates inputs of the form mm:ss or m:ss. I tested in online using online regex tester and it is working fine (passes inputs like 07:29 or 7:29).

However, when I use jQuery to validate it it always fails.

This what I have (abbreviated form):

function validateUpdateSubmit() {debugger
    var isValid = true;
    ....
    // value of "duration" below is "07:29"
    var duration = $("#tbDuration").val();
    var durationExp = new RegExp("^[0-5]?\d:[0-5]\d$");
    ...
    if (duration == '' || !durationExp.test(duration)) {
        $("#lblErrDuration").show();
        isValid = false;
    }
    return isValid;
}

I tried:

    var validDuration = (duration == durationExp.exec(duration));

But it also returns false when value is "07:29" or "7:29".

NoBullMan
  • 2,032
  • 5
  • 40
  • 93
  • Is the text being validated in exactly 07:29? I ask because ^ and $ would mean if you had a space like "07:29 " it would not match. – sniperd Mar 06 '18 at 15:17
  • I checked again and it is exactly "07:29", no space. – NoBullMan Mar 06 '18 at 15:35
  • 1
    It seems this question was indeed a duplicate of first link listed on top of the page. the problem was that I had to use \\d instead of \d in regex string. – NoBullMan Mar 06 '18 at 15:39

0 Answers0