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 and js are the same. Let's look at the example below.
JQuery is used in the 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: |
Copy code |
/** * 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: |
Copy code |
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> <Html> <Head> <Title> New Document </title> <Meta name = "Generator" content = "EditPlus"> <Meta name = "Author" content = ""> <Meta name = "Keywords" content = ""> <Meta name = "Description" content = ""> </Head> <Body> Date of birth: <input id = "birthday" name = "birthday" type = "text" value = "" onBlur = "checkDate ();"/> </Body> <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> </Html> |