You can add your own methods to jQuery.validate . Probably one of them is available for this, but as an example, I encoded this check in a 24-hour format:
$.validator.addMethod("time24", function(value, element) { if (!/^\d{2}:\d{2}:\d{2}$/.test(value)) return false; var parts = value.split(':'); if (parts[0] > 23 || parts[1] > 59 || parts[2] > 59) return false; return true; }, "Invalid time format.");
Fiddle
Documentation
You can also sum it in one regular expression ( source ):
([01]?[0-9]|2[0-3])(:[0-5][0-9]){2}
And if you use HTML5, you can use the pattern attribute:
<input type="text" pattern="([01]?[0-9]|2[0-3])(:[0-5][0-9]){2}" required="required" placeholder="hh:mm:ss" />
Fiddle
Continue to use the jQuery.validate method if you need compatibility with older browsers:
$.validator.addMethod("time24", function(value, element) { return /^([01]?[0-9]|2[0-3])(:[0-5][0-9]){2}$/.test(value); }, "Invalid time format.");
Demo
Fabrício matté
source share