Regex expression for times?

I’m trying to figure out what regex expression can check a time (MM:SS:hundreths format)
Tried
[1][0-9]:[0-9][0-9]:[0-9][0-9]$
to no avail though?
Any ideas?


  1. 0-9 ↩︎

It works for me.

var match = "12:34:56".match(/^[0-9][0-9]:[0-9][0-9]:[0-9][0-9]$/);
// match is ["12:34:56"]

is the problem that it will also accept ‘99:99:99’ as valid?

Try this to fix that problem.

[1][0-9]:[0-5][0-9]:[0-9][0-9]$


  1. 0-5 ↩︎

1 Like

isn’t 99:99:99 a valid time though?
heres the function

//countdown timer function
function timerStart() {
var start = document.getElementById("Timer_Start").value;
console.log(start);
//check format for user input
if( start.match(/^[0-9][0-9]:[0-9][0-9]:[0-9][0-9]$/)) {
	document.getElementsByClassName("Error").innerHTML = "";
} else {
	document.getElementsByClassName("Error").innerHTML = "Please use correct format (00:00:00)";
	document.getElementById("Timer_Start").focus();
}
}

and heres the result


the console.log works, but my regex test doesnt seem to

How? So me on a clock where you can have more than 59 seconds. At worst, the seconds needs to be limited to 59, I could see how one may describe 120 minutes instead of 2 hours zero minutes, zero seconds, but you can’t have 99 seconds and still have minutes be entered, such as, 1 minute 99 seconds. That doesn’t make sense…

document.getElementsByClassName returns an array-like object containing all elements with that class. You can set the .innerHTML of a specific element like

document.getElementsByClassName('Error')[0].innerHTML = 'foo';

or

// Returns the first match
document.querySelector('.Error').innerHTML = 'foo';
1 Like

ohhhh

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.