Here we verify that the date format is yyyy-mm-dd, so that when there is a fixed format of our regular expression, we can write it like this/^ (\ d {4 }) -(\ d {2})-(\ d {2}) $/to operate. I will introduce it below.
Example 1
The Code is as follows: |
Copy code |
/** Determine whether the input date format 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.exe c (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 whether the input date format 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, 10) = (1 + d. getMonth () & (parseInt (RegExp. $1, 10) = D. getDate () & (parseInt (RegExp. $3, 10) = d. getFullYear ()); }, |
Example 3
A complete Date Format function is provided here.
The Code is as follows: |
Copy code |
// Whether the date format is used Function CheckDate (strDate ){ Var reg =/^ (d {4})-(d {2})-(d {2}) $ /; If (! Reg. test (strDate )){ Alert ("the date format is incorrect! /N is in the correct format "); 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 (year, month, date) {return false ;} Return true; } Function checkYear (year ){ If (isNaN (parseInt (year ))){ Alert ("Incorrect year input. Please try again! "); Return false; } Else if (parseInt (year) <1950 | parseInt (year)> 2050 ){ Alert ("the year should be! "); Return false; } Else return true; } Function checkMonth (month ){ If (isNaN (parseInt (month, 10) {alert ("Incorrect month input. Please enter it again! "); Return false ;} Else if (parseInt (month, 10) <1 | parseInt (month, 10)> 12 ){ Alert ("the month should be between 1 and 12 "); Return false; } Else return true; } Function checkDate (year, month, date ){ Var daysOfMonth = CalDays (parseInt (year), parseInt (month )); If (isNaN (parseInt (date) {alert ("incorrect date input. Please enter it again! "); Return false ;} Else if (parseInt (date) <1 | parseInt (date)> daysOfMonth) {alert ("the 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 & amp; year % 100! = 0) | (year % 400 = 0) return true; Else return false; } |