当前位置: 首页 > 图文教程 > 网络编程 > Javascript > 网页输入框日期型有效性判定一网打尽

Javascript
动态生成select选项全接触
不刷新页面动态更新select选项,实现两个select相互操作
网页输入框日期型有效性判定一网打尽
实用Javascript函数之一(自动将输入文本框中的内容转换成大写字符)
实用Javascript函数之二(自动将输入文本框中的内容转换成小写字符)
实用Javascript函数之三(限制文本输入框中只能输入数字\"0\"到\"9\")
实用Javascript函数之四(用于对sString字符串进行前空格截除)
实用Javascript函数之五(用于对sString字符串进行后空格截除)
实用Javascript函数之六(截除字符串前后空格)
如何使用交替的滚动标题
采用DOM模型时创建一个Select节点后,要删除option项的解决方法
javascript函数速查
利用JavaScript和正则表达式进行丰富的日期判断(给其它项目组的代码,有比较好的编程风格和注释)
关于字符串的几个有用函数
FileSystemObject 的例子(处理驱动器、文件夹、文件)
用JScript实现VB.Net,C#的[委托Delegate]:
得到固定字符位置的函数
IE NC通用的藏鼠标右键一法
Menu
foolpot2001菜单

Javascript 中的 网页输入框日期型有效性判定一网打尽


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

[著者]zosatapo

[联系][email protected]

我利用正则表达式写的日期型网页输入框数据是否有效的判定函数。

包含闰年的处理,有兴趣正则表达式的同行,以及需要对网页中日期进行

很好判定的朋友可以参照一下。同时函数支持自我日期输入形式定义。

1#辅助函数(闰年判定)

function isLeapYear(year)
{
 if((year%4==0&&year%100!=0)||(year%400==0))
 {
 return true;
 } 

 return false;
}

2#判定主函数

function dateValidation(object,format)
{
 var regexp,value,index;
 var year,month,day;
 var iyear,imonth,iday;
 var fmt,regfmt,ordfmt;
 var dateArray;

 if(isObject(object))
 {
  value=object.value;
 }
 else if(isString(object)&&!isEmpty(object))
 {
  value=object;
 }
 else
 {
  return false;
 }
 if(isEmpty(format))
 {
  return false;
 }

 fmt=new Array("yyyy/mm/dd","mm/dd/yyyy","dd/mm/yyyy");
 
 regfmt=new Array("/^([0-9]{4})\\/([0-9]{2})\\/([0-9]{2})$/","/^([0-9]{2})\\/([0-9]{2})\\/([0-9]{4})$/","/^([0-9]{2})\\/([0-9]{2})\\/([0-9]{4})$/");

 ordfmt=new Array("123","312","321");

 format=format.toLowerCase();
 for(index=0;index<fmt.length;index++)
 {
  if(format==fmt[index])
  { 
   eval('regexp='+regfmt[index]+';');

   iyear=parseInt(ordfmt[index].charAt(0));
   imonth=parseInt(ordfmt[index].charAt(1));
   iday=parseInt(ordfmt[index].charAt(2));

   break;
  }
 }
 
 if(index==fmt.length)
 {
  alert("Date Format Not Supported!");
  return false;
 }

 if(regexp.test(value)){
  //alert("Date is matched with Format!");
  dateArray=value.match(regexp);

  year=dateArray[iyear];
  month=dateArray[imonth];
  day=dateArray[iday];

  //alert("The Date you have filled is:\nYear:"+year+"\nMonth:"+month+"\nDay:"+day);
  
  if(year<2001)
  {
   alert("Year must be greater than 2001!");
   return false;
  }
  if(month<0||month>12)
  {
   alert("Month must range from 1 to 12!");
   return false;
  }

  if(day<0||day>31)
  {
   alert("Day must range from 1 to 31!");
   return false;
  }
  else
  { 
   if(month==2)
   { 
    if(isLeapYear(year)&&day>29)
    {
     alert("In Month 2,Day must range from 1 to 29!");
     return false;
    }
    
    if(!isLeapYear(year)&&day>28)
    {
     alert("In Month 2,Day must range from 1 to 28!");
     return false;
    }
    
   }

   if((month==4||month==6||month==9||month==11)&&(day>30))
   {
    alert("In this Month ,Day must range from 1 to 30!");
    return false;
   }
  }

 }
 else
 {
  alert("Date isn't matched with Format!\nDate Format:"+format);
  return false;
 }

 return true;
}