Javascript verification date function

Source: Internet
Author: User

This leads to errors in database running tasks (tasks are computed based on dates, and tasks run at night due to a large amount of data). To avoid such errors, therefore, a javascript method is added to verify the validity of the date.
This method can effectively verify the leap year. Supported date formats include 2009-01-01 and 2009/01/01.
Javascript code
Copy codeThe Code is as follows:
// Determine whether the date is valid
Function IsDate (oTextbox ){
Var regex = new RegExp ("^ (? :(? :( [0-9] {4} (-| \/)(? :(? : 0? [1, 3-9] | 1 [0-2]) (-| \/)(? : 29 | 30) | ((? : 0? [13578] | 1 [02]) (-| \/) 31) | ([0-9] {4} (-| \/)(? : 0? [1-9] | 1 [0-2]) (-| \/)(? : 0? [1-9] | 1 \ d | 2 [0-8]) | (((? :( \ D (? : 0 [48] | [2468] [048] | [13579] [26]) | (? : 0 [48] 00 | [2468] [048] 00 | [13579] [26] 00) (-| \/) 0? 2 (-| \/) 29) $ ");
Var dateValue = oTextbox. value;
If (! Regex. test (dateValue )){
Alert ("date error! ");
DateValue = "";
This. focus ();
Return;
}
}

Use test
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> </title>
<Script type = "text/javascript" src = "jQuery/JScript1.js"> </script>
<Script type = "text/javascript" src = "jQuery/jquery-vsdoc.js"> </script>
</Head>
<Body>
<Input type = "text" id = "sDate"/>
<Script type = "text/javascript">
$ ("# SDate"). blur (function () {IsDate ($ (this) [0]);})
</Script>
</Body>
</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.