Home >Web Front-end >JS Tutorial >Summary of regular expressions for verifying time format in jQuery_jquery

Summary of regular expressions for verifying time format in jQuery_jquery

WBOY
WBOYOriginal
2016-05-16 17:22:011474browse

jQuery is used in the code, and the parameter passed in is the input object selected by jQuery. Principle: first use regular matching, and then extract the hours, minutes and seconds to see if they are within the normal range.

Copy code The code is as follows:

/**
* Check whether the time conforms to the format
* @param {Object} timeTextBox
*/
function checkTime(timeTextBox){
var time = timeTextBox.val();
var regTime = /^([0-2][0-9]):([0-5][0-9]) :([0-5][0-9])$/;
var result = false;
if (regTime.test(time)) {
if ((parseInt(RegExp.$1) < ; 24) && (parseInt(RegExp.$2) < 60) && (parseInt(RegExp.$3) < 60)) {
                                                                                       ) {
$ ("#ERRMSG"). Html ("");
} else {
Timetextbox.attr ({
Value: "
}); .focus();
$("#errmsg").html("Time format error");
}
return result;
}



Example 2
Directly use js regular expressions to example

Copy code








Date of Birth: < input id="birthday" name="birthday" type="text" value="" onBlur="checkDate();"/>