Below I will introduce two regular expression examples for verifying the time format. This can actually be called js directly, because the Regular Expression Function jquery is the same as js. Below we can see that jQuery is used in the instance code, the input parameter is the input object selected by jQuery. principle: first use regular expression matching, and then extract the hour, minute, and second to see if it is within the normal range.
The Code is as follows:
/**
* Check whether the time meets 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 )){
Result = true;
}
}
If (result ){
$ ("# Errmsg" cmd.html ("");
} Else {
TimeTextBox. attr ({
Value :""
});
TimeTextBox. focus ();
$ ("# Errmsg" ).html ("Time Format error ");
}
Return result;
}
Example 2
Directly use js + Regular Expressions for instances
The Code is as follows:
New Document
Date of birth:
Script
Var DATE_FORMAT =/^ [0-9] {4}-[0-1]? [0-9] {1}-[0-3]? [0-9] {1} $ /;
Function checkDate (){
Var birthday = document. getElementById ("birthday"). value;
If (DATE_FORMAT.test (birthday )){
Alert ("the date format you entered is correct ");
} Else {
Alert ("sorry, the date format you entered is incorrect. The correct format should be" 2012-01-01 ".");
}
}
Script
Jquery verification time
Set of Regular Expressions for verification time
// Date Format: yyyy
PatternsDict. date_y =/^ (\ d {4}) $ /;
// Date Format: yyyy-mm
PatternsDict. date_ym =/^ (\ d {4})-(0 \ d {1} | 1 [0-2]) $ /;
// Date Format: yyyy-mm-dd
PatternsDict. date_ymd =/^ (\ d {4})-(0 \ d {1} | 1 [0-2]) -(0 \ d {1} | [12] \ d {1} | 3 [01]) $ /;
// Time format hh
PatternsDict. time_h =/^ (0 \ d {1} | 1 \ d {1} | 2 [0-3]) $ /;
// Time format hh: mm
PatternsDict. time_hm =/^ (0 \ d {1} | 1 \ d {1} | 2 [0-3]) :( [0-5] \ d {1 }) $ /;
// Time format hh: mm: ss
PatternsDict. time_hms =/^ (0 \ d {1} | 1 \ d {1} | 2 [0-3]): [0-5] \ d {1} :( [0-5] \ d {1}) $ /;
The format is time, that is, the first two digits cannot be less than 23, and the time and minute behind cannot be greater than 59. It must be written at 16:02:01 instead.
[0-2] [0-3]: [0-5] [0-9]: [0-5] [0-9]
Use CompareValidator
Set operator to DateTypeCheck
Set type to Date
Here is to judge the format of YYYY-MM-DD, basically the leap year and February and so on are taken into account, but I have forgotten where to find.
^ (1 [6-9] | [2-9] \ d) \ d {2})-(0? [1, 13578] | 1 [02])-(0? [1-9] | [12] \ d | 3 [01]) | (1 [6-9] | [2-9] \ d) \ d {2})-(0? [13456789] | 1 [012])-(0? [1-9] | [12] \ d | 30) | (1 [6-9] | [2-9] \ d) \ d {2 }) -0? 2-(0? [1-9] | 1 \ d | 2 [0-8]) | (1 [6-9] | [2-9] \ d) (0 [48] | [2468] [048] | [13579] [26]) | (16 | [2468] [048] | [3579] [26]) 00)-0? 2-29-) $
The following is a time verification
^ (1 [6-9] | [2-9] \ d) \ d {2})-(0? [1, 13578] | 1 [02])-(0? [1-9] | [12] \ d | 3 [01]) | (1 [6-9] | [2-9] \ d) \ d {2})-(0? [13456789] | 1 [012])-(0? [1-9] | [12] \ d | 30) | (1 [6-9] | [2-9] \ d) \ d {2 }) -0? 2-(0? [1-9] | 1 \ d | 2 [0-8]) | (1 [6-9] | [2-9] \ d) (0 [48] | [2468] [048] | [13579] [26]) | (16 | [2468] [048] | [3579] [26]) 00)-0? 2-29-) (20 | 21 | 22 | 23 | [0-1]? \ D): [0-5]? \ D: [0-5]? \ D $
[Aspx page: <% # DataBinder. Eval (Container. DataItem, "Company_Ureg_Date") %>
Shown as: 19:44:28
I only want]
<% # DataBinder. Eval (Container. DataItem, "Company_Ureg_Date", "{0: yyyy-M-d}") %>
How should I change it?
[Format date]
It is usually an object.
(DateTime) objectFromDB). ToString ("yyyy-MM-dd ");
A. The correct input format is as follows: [], [10:29:39], []
^ (\ D {2} ([02468] [048]) | ([13579] [26]) [\-\/\ s]? (0? [13578]) | (1 [02]) [\-\/\ s]? (0? [1-9]) | ([1-2] [0-9]) |
(3 [01]) | (0? [469]) | (11) [\-\/\ s]? (0? [1-9]) | ([1-2] [0-9]) | (30) | (0? 2 [\-\/\ s]? (0? [1-9]) | ([1-2] [0-9])
| (\ D {2} ([02468] [1235679]) | ([13579] [01345789]) [\-\/\ s]? (0? [13578]) | (1 [02]) [\-\/\ s]? (0? [1-9]) | ([1-2] [0-9])
| (3 [01]) | (0? [469]) | (11) [\-\/\ s]? (0? [1-9]) | ([1-2] [0-9]) | (30) | (0? 2 [\-\/\ s]? (0? [1-9]) | (1 [0-9]) | (2 [0-8]) (\ s (0? [1-9]) | (1 [0-2]) \ :( [0-5] [0-9]) (\ s) | (\ :( [0-5] [0-9]) \ s) ([AM | PM | am | pm] {2, 2 })))? $
B. The following correct input format: [0001-12-31], [9999 09 30], [2002/03/03]
^ \ D {4} [\-\/\ s]? (0 [13578]) | (1 [02]) [\-\/\ s]? ([0-2] [0-9]) | (3 [01]) | (0 [469]) | (11 )) [\-\/\ s]? ([0-2] [0-9]) | (30) | (02 [\-\/\ s]? [0-2] [0-9]) $