JavaScript regular judgment Date format instance

Source: Internet
Author: User
Tags getdate


Example 1

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

/**
Determine the date format entered in the input box is YYYY/MM/DD and the correct date
*/
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 ());
},

Example 3

Here we have a more complete date format function

The code is as follows Copy Code
Whether it is a date format
function Checkdate (strdate) {
var reg =/^ (d{4})-(D{2})-(d{2}) $/;
if (!reg.test (strdate)) {
Alert ("date format is incorrect!/n the correct format is: 2004-01-01");
return false;
}
var year = strdate.substring (0, 4);
var month = strdate.substring (5, 7);
var date = strdate.substring (8, 10);
if (!checkyear (year)) {return false;}
if (!checkmonth (month)) {return false;}
if (!checkdate, month, date)) {return false;}
return true;
}
function Checkyear (year) {
if (isNaN (parseint (year)) {
Alert ("The year entered incorrectly, please re-enter!");
return false;
}
else if (parseint) < 1950 | | parseint (YEAR) > 2050) {
Alert ("Year should be between 1950-2050!");
return false;
}
else return true;
}
function Checkmonth (month) {
if (parseint (month)) {alert ("The month entered incorrectly, please re-enter!");
else if (parseint (month) < 1 | | | parseint (month, ten) > 12) {
Alert ("Month should be between 1-12");
return false;
}
else return true;
}
function checkdate (year, month, date) {
var daysofmonth = caldays (parseint (year), parseint (month));
if (isNaN (parseint (date)) {alert ("Enter the date incorrectly, please re-enter!");
else if (parseint (date) < 1 | | parseint (DATE) > Daysofmonth) {alert ("date should be between 1-" + Daysofmonth + "); return false; }
else return true;
}
Function caldays (year, month) {
var date = new Date (year, month, 0);
return Date.getdate ();
}
function Isleapyear (year) {
if (year% 4 = 0 && Year% 100!= 0) | | (Year% 400 = 0)) return true;
else return false;
}
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.