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