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

Javascript
实现表格中行点击时的渐扩效果!
多图展示滑动过渡效果
弹出自适应图片大小的窗口弹出窗口根据图片大小,自动判断高和宽。
拖动Html元素集合 Drag and Drop any item
脚本吧 - 幻宇工作室用到js,超强推荐base.js
使用透明叠加法美化文件上传界面
JavaScript高级程序设计
对象的类型:本地对象(1)
如何实现表格中行点击时的渐扩效果!
使用button标签,实现三态图片按钮
根据分辩率调用不同的CSS.
Web版彷 Visual Studio 2003 颜色选择器
JS中简单的实现像C#中using功能(有源码下载)
js之WEB开发调试利器:Firebug 下载
jQuery中文入门指南,翻译加实例,jQuery的起点教程
jQuery 1.0.4 - New Wave Javascript(js源文件)
强悍无比的WEB开发好助手FireBug(Firefox Plugin)
换肤测试程序js脚本
xWin之JS版(2-26更新)
共享自己写一个框架DreamScript

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-10   浏览: 338 ::
收藏到网摘: 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;
}