And the use of JavaScript regular expressions to determine the date of the rich (the code to other project team, have good programming style and comments)

  (Eg: 2001-3-7 2001/3/7 2001.3.7)
Year, month, day must complete
Japan can not
Month and day can not
<Script language = javascript src = "Scroll.js"> // *********************************** ************************************************** ********************* function fnOnclick () {var strDate = fnRemoveBrank (document.all.cnlDate.value); var intCheckRadio = 0; if (document.all .cnlRadio [1] .checked) {intCheckRadio = 1;} else if (document.all.cnlRadio [2] .checked) {intCheckRadio = 2;}! if (fnCheckDate (strDate, intCheckRadio)) {alert ( "date does not legal ");} else {alert (" valid date ");}} // ******************************* ************************************************** ************************************************************ // author: between the date of the check function, supports three-year, month, day: Lin Manxing // function delimiter "-", ".", and "/" can select the year, month, day whether it should complete. // correct date format: 2001-2-132001 2001-2 2001.2.13 2001. 2 2001/2/3, date range from 1-1-1 to 9999-12-31 // Meanwhile, the number of days of the current month of the current year also made a judgment, such as: all 2001-2-29 2001-4-31 // parameters are illegal date: strDate ---- need to determine the date string // intFlag: 1 ---- ---- February can not be no day and date must be months 0 ---- // return value range: true ---- ---- date to false lawfully illegal function fnCheckDate (strDate, intFlag) {var strCheckDate = strDate + ""; // but where further confirmed character string is definitely determined string if (strCheckDate == "") // null string, the string is not a valid date, returns false {return false;} // Analyzing the data passed in is written in that format date var intIndex = -1; // use regular expressions to find the string contains a character not found -1, otherwise (0 - String.length - 1) var arrDate; // store date var regExpInfo = //./; . "" // regular expression matching the first to appear // here, the reason I do not use all the functions and replace "/" with ".", "-", and then were stored in Day, because the user might enter 2001 / 3-2, we can not judge that it is not a legitimate date intIndex = strCheckDate.search (regExpInfo); // find whether it contains if (intIndex == - 1) / "." / does not contain {regExpInfo = / - /; intIndex = strCheckDate.search (regExpInfo);

 

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

Source code is as follows:

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

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
<input name="cnlDate" id="cnlDate" value="">(如:2001-3-7 2001/3/7 2001.3.7)

<br>
<input name="cnlRadio" id="Radio" value="0" type="radio" checked>年、月、日必须齐全<br>
<input name="cnlRadio" id="Radio" value="1" type="radio">可以没有日<br>
<input name="cnlRadio" id="Radio" value="2" type="radio">可以没有月和日<br>

<input name="cnlButton" name="cnlButton" type="button" value="日期检查" οnclick="fnOnclick()">
</BODY>
</HTML>
<script language="javascript" src="Scroll.js">
//**********************************************************************************************************
function fnOnclick()
{
 var strDate = fnRemoveBrank(document.all.cnlDate.value);
 var intCheckRadio = 0;
 
 if(document.all.cnlRadio[1].checked)
 {
  intCheckRadio = 1;
 }
 else if(document.all.cnlRadio[2].checked)
 {
  intCheckRadio = 2;
 }
 
 if(!fnCheckDate(strDate,intCheckRadio))  { the else }  Alert ( "invalid date");
 {




  alert ( "valid date");
 }
}

// **************************** ************************************************** ********
// function: date check function, supports three years, a separator between the month, day "-" and "/" can select the year, month, day whether it should complete. "" .
// correct date format: 2001-2-132001 2001-2 2001.2.13 2001.2 2001/2/3, date range from 1-1-1 to 9999-12-31
// At the same time, the current month of the current year the judge also made a number of days, such as: 2001-2-29 2001-4-31 illegal date
// parameters: strDate ---- need to determine the date string
// intFlag: 1 ---- can not February can not ---- 0 ---- day and month date must complete
// return value: true ---- lawfully false ---- date illegal
function fnCheckDate (strDate, INTFLAG)
{
 var strCheckDate = strDate + ""; // but where further confirmed character string is a series of affirmative determination
 
 if (strCheckDate == "") // null string, the string is not a valid date, returns to false
 {
  return to false;
 } 
 
 / / judgment is passed in the data written in that format date
 var intIndex = -1; // use regular expressions to find the string contains a character not found -1, otherwise (0 - String.length - 1)
 var arrDate; // store date
 "."; var regExpInfo = //./ // regular expression matching the first to appear
 
 // here, the reason I do not use all the functions and replace "/" with "-". " "and then store the date, because the user is likely to enter the 2001 / 3-2, you can not judge that it is not a legitimate date
 intIndex = strCheckDate.search (regExpInfo); // Find if they contain". "
 iF ( intIndex == - 1) // does not contain  
 {
  regExpInfo = //;
  intIndex = strCheckDate.search (regExpInfo);
  
  iF (intIndex == -1)
  {
   regExpInfo = ////; // find whether it contains "/"
   = strCheckDate.search intIndex (regExpInfo); 
   
   IF (intIndex == -1)
   {
    arrDate the Array = new new (strCheckDate);// contains only of
   }
   the else
   {
    arrDate = strCheckDate.split ( "/") ; // 2001/3/7 type
   }
  }
  the else
  {
   arrDate = strCheckDate.split ( "-"); // 2001-3-7 type
  }
 }
 the else
 {
  arrDate = strCheckDate. split ( "."); //2001.3.7 type
 }
 
 IF (arrDate.length>. 3) // if more than three separate items, in addition there are other, not legal date date, returns to false
 {
  return to false ;
 }
 the else iF (arrDate.length> 0)
 {
  // determines whether legitimate
  if (fnIsIntNum (arrDate [0] )) // is a positive integer
  {
   iF (the parseInt (arrDate [0]) <. 1 || the parseInt (arrDate [ 0])> 9999) in the range of //. 1 - 9999
   {
    return to false;
   } 
  }
  the else
  {
   return false; // not a positive integer of, error
  }
   
  if valid month @ Analyzing
  IF (arrDate.length>. 1)
  {
   IF (fnIsIntNum (arrDate [. 1])) // is a positive integer
   {
    IF (the parseInt (arrDate [. 1] ) <. 1 || the parseInt (arrDate [. 1])> 12 is)
    {
     return to false;
    } 
   }
   the else
   {
    return to false;
   }
  }
  the else // no monthly
  {
   ! IF (INTFLAG = 2) have to have // month
   {
    return to false ;
   }
  }
   
  // determines whether the valid date
  iF (arrDate.length> 2)
  {
   iF (fnIsIntNum (arrDate [2])) // is a positive integer
   {
    var intDayCount = fnComputerDay(parseInt(arrDate[0]),parseInt(arrDate[1]));
    if(intDayCount < parseInt(arrDate[2]))
    {
     return false;
    }   
   }
   else
   {
    return false;
   }
  }
  else
  {
   if(intFlag == 0)    //必须得有日
   {
    return false;
   }
  }
 }
 return true;
}

// **************************** ************************************************** ********
// determines whether the number is a positive integer
@ parameters: strNum ---- need to determine the string
// return value: true ---- ---- integer to false non-integer
function fnIsIntNum (strNum)
{
 var strCheckNum strNum + = "";
 IF (strCheckNum.length <. 1) // empty string
  return to false;
 the else IF (isNaN (strCheckNum)) // not a numerical value
  return to false;
 the else IF (the parseInt (strCheckNum ) <1) // not positive
  return to false; 
 the else IF (parseFloat (strCheckNum)> the parseInt (strCheckNum)) is not an integer // 
  return to false;
 
 return to true;
}

// **************************** ************************************************** ********
// function: determining the number of days on intMonth intYear month
// returns: the number of days of the month intYear on intMonth
function fnComputerDay (intYear, intMonth)
{
    var = new new dtmDate a Date (intYear, intMonth, -1 );
    var intDay dtmDate.getDate = () +. 1;
   
    return intDay;   
}

// **************************** ************************************************** ******** // function: removing the spaces before and after the string
// return value: after removing the string of spaces
function fnRemoveBrank (strSource)
{
 return strSource.replace (/ ^ / S * /, ''). Replace (S * $ // /, '');
}
</ Script>

Released six original articles · won praise 2 · Views 7956

Guess you like

Origin blog.csdn.net/Soft21/article/details/13037