利用JavaScript和正则表达式进行丰富的日期判断(给其它项目组的代码,有比较好的编程风格和注释)

  (如:2001-3-7 2001/3/7 2001.3.7)
年、月、日必须齐全
可以没有日
可以没有月和日
<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("日期不合法"); } else { alert("日期合法"); } } //********************************************************************************************************** //作者:林曼杏 //功能:日期检查函数,支持3种年、月、日之间的分隔符 "-"、"."和"/"可以选择年、月、日是否应该完整。 // 正确的日期格式为:2001-2-13 2001 2001-2 2001.2.13 2001.2 2001/2/3,日期范围为 1-1-1 到 9999-12-31 // 同时,对当前年当前月的天数也做了判断,如:2001-2-29 2001-4-31 都是非法的日期 //参数:strDate ---- 需要判断的日期字符串 // intFlag: 1 ---- 可以没有日 2 ---- 可以没有日和月 0 ---- 年月日必须齐全 //返回值:true ---- 日期合法 false ---- 日期不合法 function fnCheckDate(strDate,intFlag) { var strCheckDate = strDate + ""; //进一步确认哪来判断的肯定是一串字符串 if(strCheckDate == "") //空字符串,不是合法的日期字符串,返回false { return false; } //判断传进来的数据是那种格式写成日期 var intIndex = -1; //利用正则表达式,查找字符串中是否包含某个字符,没找到为-1,否则为 (0 - String.length - 1) var arrDate; //分别存储年月日 var regExpInfo = //./; //正则表达式,匹配第一个出现 "."的位置 //在这里,我之所以不使用replace函数把所有的"."和"/"换成"-",然后分别存储年月日,是因为用户有可能输入 2001/3-2,就判断不出它是不合法日期了 intIndex = strCheckDate.search(regExpInfo); //查找是否含有 "." if(intIndex == - 1) //不包含 { regExpInfo = /-/; intIndex = strCheckDate.search(regExpInfo); if(intIndex == -1) { regExpInfo = ////; //查找是否含有 "/" intIndex = strCheckDate.search(regExpInfo); if(intIndex == -1) { arrDate = new Array(strCheckDate); //只包含年 } 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) //如果分离出来的项超过3,除了年月日还有其它的,不合法日期,返回false { return false; } else if(arrDate.length > 0) { //判断年是否合法 if(fnIsIntNum(arrDate[0])) //是正整数 { if(parseInt(arrDate[0]) < 1 || parseInt(arrDate[0]) > 9999) //年范围为1 - 9999 { return false; } } else { return false; //年不是正整数,错误 } //判断月是否合法 if(arrDate.length > 1) { if(fnIsIntNum(arrDate[1])) //是正整数 { if(parseInt(arrDate[1]) < 1 || parseInt(arrDate[1]) > 12) { return false; } } else { return false; } } else //没有月 { if(intFlag != 2) //必须得有月 { return false; } } //判断日是否合法 if(arrDate.length > 2) { if(fnIsIntNum(arrDate[2])) //是正整数 { 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; } //********************************************************************************************************** //判断一个数是否为正整数 //参数:strNum ---- 需要判断的字符串 //返回值:true ---- 整数 false ---- 非整数 function fnIsIntNum(strNum) { var strCheckNum = strNum + ""; if(strCheckNum.length < 1) //空字符串 return false; else if(isNaN(strCheckNum)) //不是数值 return false; else if(parseInt(strCheckNum) < 1) //不是正数 return false; else if(parseFloat(strCheckNum) > parseInt(strCheckNum)) //不是整数 return false; return true; } //********************************************************************************************************** //作者:连桦 //功能:判断intYear年intMonth月的天数 //返回值:intYear年intMonth月的天数 function fnComputerDay(intYear,intMonth) { var dtmDate = new Date(intYear,intMonth,-1); var intDay = dtmDate.getDate() + 1; return intDay; } //********************************************************************************************************** //作者:刘勃 //功能:去掉字符串前后空格 //返回值:去掉空格后的字符串 function fnRemoveBrank(strSource) { return strSource.replace(/^/s*/,'').replace(//s*$/,''); } </script>

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

源代码如下:

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

<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))
 {
  alert("日期不合法");
 }
 else
 {
  alert("日期合法");
 }
}

//**********************************************************************************************************
//功能:日期检查函数,支持3种年、月、日之间的分隔符 "-"、"."和"/"可以选择年、月、日是否应该完整。
//  正确的日期格式为:2001-2-13 2001 2001-2 2001.2.13  2001.2 2001/2/3,日期范围为 1-1-1 到 9999-12-31
//  同时,对当前年当前月的天数也做了判断,如:2001-2-29 2001-4-31 都是非法的日期
//参数:strDate ---- 需要判断的日期字符串
//  intFlag: 1 ---- 可以没有日  2 ---- 可以没有日和月 0 ---- 年月日必须齐全
//返回值:true ---- 日期合法 false ---- 日期不合法
function fnCheckDate(strDate,intFlag)
{
 var strCheckDate = strDate + "";     //进一步确认哪来判断的肯定是一串字符串
 
 if(strCheckDate == "")        //空字符串,不是合法的日期字符串,返回false
 {
  return false;
 } 
 
 //判断传进来的数据是那种格式写成日期
 var intIndex = -1;         //利用正则表达式,查找字符串中是否包含某个字符,没找到为-1,否则为 (0 - String.length - 1)
 var arrDate;          //分别存储年月日
 var regExpInfo = //./;        //正则表达式,匹配第一个出现 "."的位置
 
 //在这里,我之所以不使用replace函数把所有的"."和"/"换成"-",然后分别存储年月日,是因为用户有可能输入 2001/3-2,就判断不出它是不合法日期了
 intIndex = strCheckDate.search(regExpInfo);   //查找是否含有 "."
 if(intIndex == - 1)         //不包含  
 {
  regExpInfo = /-/;
  intIndex = strCheckDate.search(regExpInfo);
  
  if(intIndex == -1)
  {
   regExpInfo = ////;       //查找是否含有 "/"
   intIndex = strCheckDate.search(regExpInfo); 
   
   if(intIndex == -1)
   {
    arrDate = new Array(strCheckDate);  //只包含年
   }
   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)        //如果分离出来的项超过3,除了年月日还有其它的,不合法日期,返回false
 {
  return false;
 }
 else if(arrDate.length > 0)
 {
  //判断年是否合法
  if(fnIsIntNum(arrDate[0]))   //是正整数
  {
   if(parseInt(arrDate[0]) < 1 || parseInt(arrDate[0]) > 9999)  //年范围为1 - 9999
   {
    return false;
   } 
  }
  else
  {
   return false;     //年不是正整数,错误
  }
   
  //判断月是否合法
  if(arrDate.length > 1)
  {
   if(fnIsIntNum(arrDate[1]))  //是正整数
   {
    if(parseInt(arrDate[1]) < 1 || parseInt(arrDate[1]) > 12)
    {
     return false;
    } 
   }
   else
   {
    return false;
   }
  }
  else //没有月
  {
   if(intFlag != 2)    //必须得有月
   {
    return false;
   }
  }
   
  //判断日是否合法
  if(arrDate.length > 2)
  {
   if(fnIsIntNum(arrDate[2]))  //是正整数
   {
    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;
}

//**********************************************************************************************************
//判断一个数是否为正整数
//参数:strNum ---- 需要判断的字符串
//返回值:true ---- 整数 false ---- 非整数
function fnIsIntNum(strNum)
{
 var strCheckNum = strNum + "";
 if(strCheckNum.length < 1)         //空字符串
  return false;
 else if(isNaN(strCheckNum))         //不是数值
  return false;
 else if(parseInt(strCheckNum) < 1)       //不是正数
  return false; 
 else if(parseFloat(strCheckNum) > parseInt(strCheckNum)) //不是整数 
  return false;
 
 return true;
}

//**********************************************************************************************************
//功能:判断intYear年intMonth月的天数
//返回值:intYear年intMonth月的天数
function fnComputerDay(intYear,intMonth)
{
    var dtmDate = new Date(intYear,intMonth,-1);
    var intDay = dtmDate.getDate() + 1;
   
    return intDay;   
}

扫描二维码关注公众号,回复: 9884237 查看本文章

//********************************************************************************************************** //功能:去掉字符串前后空格
//返回值:去掉空格后的字符串
function fnRemoveBrank(strSource)
{
 return strSource.replace(/^/s*/,'').replace(//s*$/,'');
}
</script>

发布了6 篇原创文章 · 获赞 2 · 访问量 7956

猜你喜欢

转载自blog.csdn.net/Soft21/article/details/13037