当前位置: 首页 > 图文教程 > 网络编程 > Javascript > 利用JavaScript和正则表达式进行丰富的日期判断(给其它项目组的代码,有比较好的编程风格和注释)

Javascript
JQuery 获取和设置Select选项的代码
jQuery ctrl+Enter shift+Enter实现代码
jQuery学习2 选择器的使用说明
jQuery学习3:操作元素属性和特性
jQuery学习4 浏览器的事件模型
jQuery学习5 jQuery事件模型
jQuery 学习6 操纵元素显示效果的函数
jQuery学习7 操作JavaScript对象和集合的函数
jQuery库与其他JS库冲突的解决办法
JavaScript Event事件学习第一章 Event介绍
JavaScript Event学习第二章 Event浏览器兼容性
JavaScript Event学习第三章 早期的事件处理程序
JavaScript Event学习第四章 传统的事件注册模型
JavaScript Event学习第五章 高级事件注册模型
JavaScript Event学习第六章 事件的访问
JavaScript Event学习第七章 事件属性
JavaScript Event学习第八章 事件的顺序
js png图片(有含有透明)在IE6中为什么不透明了
JavaScript 读取元素的CSS信息的代码
基于mootools的圆角边框扩展代码

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-10   浏览: 223 ::
收藏到网摘: n/a

  (如:2001-3-7 2001/3/7 2001.3.7)
年、月、日必须齐全
可以没有日
可以没有月和日

 

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

源代码如下:

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

<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="日期检查" onclick="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;   
}

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