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".

3
  • 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. Commented Mar 6, 2018 at 15:17
  • I checked again and it is exactly "07:29", no space. Commented Mar 6, 2018 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. Commented Mar 6, 2018 at 15:39

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.