Home >Web Front-end >JS Tutorial >Summary of regular expressions for verifying time format in jQuery_jquery
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
A collection of regular expressions to verify time
//Date format yyyy
PatternsDict.date_y= /^(d{4})$/;
//Date format yyyy-mm
PatternsDict.date_ym= /^(d{4})-(0d{1}|1[0-2])$/;
//Date format yyyy-mm-dd
PatternsDict.date_ymd= /^(d{4})-(0d{1}|1[0-2])-(0d{1}|[12 ]d{1}|3[01])$/;
//Time format hh
PatternsDict.time_h=/^(0d{1}|1d{1}|2[0-3])$/;
//Time format hh:mm
PatternsDict.time_hm=/^(0d{1}|1d{1}|2[0-3]):([0-5]d{1})$ /;
//Time format hh:mm:ss
PatternsDict.time_hms=/^(0d{1}|1d{1}|2[0-3]):[0-5]d{1}: ([0-5]d{1})$/;
The format is time, which means that the first two digits cannot be less and cannot be greater than 23, and the following hours and minutes cannot be greater than 59. They must be written in 16:02:01 and not 16:2:1
[0-2][0-3]:[0-5][0-9]:[0-5][0-9]
Use CompareValidator
Set operator to DateTypeCheck
Set type to Date
Here is the format YYYY-MM-DD, which basically takes into account leap years and February, but I have forgotten where I found it.
^((((1[6-9]|[2-9]d)d{2})-(0?[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 ]|1d|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 with time verification added
^((((1[6-9]|[2-9]d)d{2})-(0?[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 ]|1d|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$
[In aspx page: <%# DataBinder.Eval(Container.DataItem,"Company_Ureg_Date")%>
Displayed as: 2004-8-11 19:44:28
Me Only want: 2004-8-11 ]
<%# DataBinder.Eval(Container.DataItem,"Company_Ureg_Date","{0:yyyy-M-d}")%>
How should I change it?
[Format date]
Take it out, usually object
((DateTime)objectFromDB).ToString("yyyy-MM-dd");
A. The following correct input format: [2004-2-29], [2004-02-29 10:29:39 pm], [2004/12/31]
^((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 is 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]))$