JQuery verifies Regular Expression instances in time format

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.