Javascript code to match 24 hour format hh:mm

Here is a simple Javascript time checker.

Javascript code:

$(document).ready(function() {
$("#checkTime").submit( function() {
if(!$("#timeElementId").val().match(/^([0-1][0-9]|2[0-3])(\D*)([0-5][0-9])$/))
{
var message = 'Time should be in the following format
' +
'hh:mm, hh:00-23, mm:00-59';
alert(message);
return false;
}
}
}

HTML code:

The expression inside the match function is good enough for a pattern within a html5 field validation.
Neat HTML5 code (at this moment not working with all browsers):


Categories