Use JavaScript and regular expressions to perform rich date judgment

xiaoxiao2021-03-06  61

============================================================================================================================================================================================================= =========

The source code is as follows:

============================================================================================================================================================================================================= =========

year, month, day must complete
No day
No monthly and day

// The correct date format is: 2001-2-13 2001 2001-2 2001.2.13 2001.2 2001/2/3, the date range is 1-1-1 to 9999-12-31 / / At the same time, the current year The number of days is also judged, such as: 2001-2-29 2001-4-31 is illegal date // Parameters: strdate ---- Date string required to judge // intFlag: 1 ---- No Day 2 ---- You can not complete the day and month 0 ---- Annual month must be complete // return value: True ---- Date legality False --- Date inequality Function Fncheckdate (strdate, intflag) {var Strcheckdate = strdate "" "; // further confirm which to determine where a string IF (strcheckdate ==") // empty string, is not a legal date string, return false {returnaf false;} / / Judgment the data is the format write date var intIndex = -1; // Using the regular expression, find if a character is contained in the string, not found to -1, otherwise (0 - string.length - 1) VAR arrdate; // Save the annual month VAR regexpinfo = //./; // Regular expression, match the first appearance "." Position // Here, I don't use the replace function to put all "." And "/" change to "-", then store the annual month, because the user is likely to enter 2001 / 3-2, it is impossible to judge that it is not legal date Intindex = Strcheckdate.Search (Regexpinfo ); // look for "." If (intIndex == - 1) // does not contain {regexpinfo = / - /; intIndex = StrCheckdate.search (regexpinfo); if (intIndex == -1) {regExpinfo =; / / Find if "/" intindex = Strcheckdate.Search (regexpinfo); if (intIndex == -1) {Arrdate = New Array (StrCheckdate); // only contains annual} else {Arrdate = strCheckdate.split ("/"); // 2001/3/7}} else {arrdate = strCheckdate.split (" - "); // 2001-3-7}} else {arrdate = Strcheckdate.split (". "); //2001.3.7} if (arrdate.length> 3) // If the separated item exceeds 3. In addition to the date and month, there are other, illegal date, return false {return false;} else if (Arrdate.Length>

0) {// Judgment whether the year is legal if (FnisintNum (Arrdate [0])) // is positive integer {IF (paraSeint (Arrdate [0]) <1 || PARSEINT (Arrdate [0])> 9999) // year The range is 1 - 9999 {RETURN FALSE;}} else {return false; // year is not a positive integer, error} // judgment whether the month is legal if (ArrDate.Length> 1) {if (FnisintNum (Arrdate [1])) // is positive integer {IF (paraSeint (Arrdate [1]) <1 || parseint (arrdate [1])> 12) {Return false;}} else {return false;}} else // no month {ified (intflag) ! = 2) // must have the month {Return false;}} // judgment whether the day is legal if (ArrDate.Length> 2) {if (FnisintNum (Arrdate [2])) // is positive integer {var intdaycount = Fncomputerday (Parseint (Arrdate [0]), PARSEINT (Arrdate [1])); IF (INTDAYCOUNT PARSEINT (STRCHECKNUM)) // is not an integer return false; return true;

// ******************************************************** *********************************************************** ******** // Function: Judgment INTYEAR Years INTMONTH month // Return Value: INTYEAR INTMONTH Month Number Function Fncomputeday (Interedation "{var DTMDATE = New Date (intYear, Intmonth, -1 ); Var intday = dtmdate.getdate () 1; return INTDAY;} // ****************************************** *********************************************************** ************************ / / Function: Remove the string before and after the return value: Remove the String Function Fnremovebrank (strsource) after the space {RETURN STRSOURCE.REPLACE (/ ^ / s * /, ''). Replace (// s * $ /, '');}

转载请注明原文地址:https://www.9cbs.com/read-112582.html

New Post(0)