Date format regular expression JavaScript code

Source: Internet
Author: User

Format is day/month/year

The code is as follows Copy Code

Date format
[' Date_au ', function (v) {
if (vanadium.validators_types[' empty '].test (v)) return true;

var regex =/^ (d{2})/(D{2})/(d{4}) $/;
if (!regex.test (v)) return false;
var d = new Date (V.replace (Regex, ' $2/$1/$3 '));
Return (parseint (regexp.$2) = = (1 + d.getmonth ()) && (parseint (regexp.$1, 10) = =

D.getdate ()) && (parseint (regexp.$3,) = = D.getfullyear ());
},


' Please enter the correct date format, for example: 28/05/2010. '],

How to change the format to adult-month-day, or month-day-year

The code is as follows Copy Code

/**
Determine the date format entered in the input box is YYYY-MM-DD and the correct date
*/
function IsDate (sm,mystring) {
var reg =/^ (d{4})-(D{2})-(d{2}) $/;
var str = mystring;
var arr = reg.exec (str);
if (str== "") return true;
if (!reg.test (str) &&regexp.$2<=12&&regexp.$3<=31) {
Alert ("Please ensure that the date format entered in +sm+ is YYYY-MM-DD or the correct date!");
return false;
}
return true;
}

Example 2

The code is as follows Copy Code

Check date format, valid format for example: 2000-10-29, month and day must be two bits, can detect leap years.
function IsDate (str) {
var reg=/^ (?:(?! 0000) [0-9]{4}-(?:(?: 0 [1-9]|1[0-2])-(?: 0 [1-9]|1[0-9]|2[0-8]) | (?: 0 [13-9]|1[0-2])-

(?: 29|30) | (?: 0 [13578]|1[02])-31 | (?: [0-9]{2} (?: 0 [48]| [2468] [048]| [13579] [26]) | (?: 0 [48]| [2468] [048]|

[13579] [26]) 00)-02-29 $/;
if (Reg.test (str)) return true;
return false;
}

Example 3

Only allow input to YYYY-MM-DD format.

  code is as follows copy code

<script type= "Text/javascript"
Function checkdate ()
{
var r =/^(?:(?! 0000) [0-9]{4}-(?:(?: 0 [1-9]|1[0-2])-(?: 0 [1-9]|1[0-9]|2[0-8]) | (?: 0 [13-9]|1[0-2])-

(?: 29|30) | (?: 0 [13578]|1[02])-31 | (?: [0-9]{2} (?: 0 [48]| [2468] [048]| [13579] [26]) | (?: 0 [48]| [2468] [048]|

[13579][26]) 00)-02-29 $/;
if (!r.exec document.getElementById ("Text2″). Value") {
Alert ("Please enter a date with the correct format NR date format: yyyy-mm-ddnr example such as: 2010-08-08nr ");
return false;
}
Else {
return true;
}
}
</script>
JS Decision date format: <br/>
<input id= "text2″type=" text/><br/>
< Input id= "button2″type=" button "value=" Judgment Date "onclick=" checkdate (); " />

For more information, refer to http://www.111cn.net/wy/99/33176.htm

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.