现在的位置: 首页 > 综合 > 正文

Useful Js staples-DateEntry.js

2012年10月26日 ⁄ 综合 ⁄ 共 3784字 ⁄ 字号 评论关闭
function: validate the "2000-01-01" format date entry only.
  1        //function:check date entry
  2        function f_checkDateEntry(obj)
  3        {
  4            var dValue=obj.value;
  5            var dobjValue; //dValue's Date Object
  6            var fValue;//formatted value-string
  7            var flag;
  8            var day,day1,m,m1,y,y1;//get date's day and month and year
  9            if(dValue.length==0)
 10            {
 11                alert("日期不能为空");
 12                obj.focus();
 13                DateIsValid=false;
 14                return;
 15            }

 16            try
 17            {
 18            //天数部分和月份的判断
 19            day1=new Number(dValue.substring(dValue.lastIndexOf("-")+1));
 20            y1=dValue.substring(0,dValue.indexOf("-"));
 21            fValue=dValue.substring(dValue.indexOf("-")+1)+"-"+dValue.substring(0,dValue.indexOf("-"));
 22            m1=new Number(fValue.substring(0,fValue.indexOf("-")));
 23            dobjValue=new Date(fValue);
 24            day=dobjValue.getDate();
 25            m=dobjValue.getMonth();
 26            y=dobjValue.getFullYear();
 27            if(y.toString()!=y1)
 28            {
 29                alert('日期非法!');
 30                obj.style.color="red";
 31                obj.select();
 32                obj.focus();
 33                DateIsValid=false;
 34                return;
 35            }

 36            if(m!=(m1-1))
 37            {
 38                alert('日期非法!');
 39                obj.style.color="red";
 40                obj.select();
 41                obj.focus();
 42                DateIsValid=false;
 43                return;
 44            }

 45            if(day!=day1)
 46            {
 47                alert('日期非法!');
 48                obj.style.color="red";
 49                obj.select();
 50                obj.focus();
 51                DateIsValid=false;
 52                return;
 53            }

 54            
 55            
 56            flag=Date.parse(fValue);
 57            if(flag>=0)
 58            {
 59                obj.style.color="green";
 60                DateIsValid=true;
 61            }

 62            else
 63            {
 64                obj.style.color="red";
 65                alert('日期格式有误');
 66                obj.select();
 67                obj.focus();
 68                DateIsValid=false;
 69                return
 70            }

 71            }

 72            catch(e)
 73            {
 74                alert('日期错误'); 
 75                obj.style.color="red";
 76                obj.select();
 77                obj.focus();
 78                DateIsValid=false;
 79                return;
 80            }

 81        }

 82        //function:compare two day object and submit
 83        function f_compareDates(obj1,obj2)
 84        {    
 85            var dValue1=obj1.value;
 86            var dValue2=obj2.value;
 87            var fValue1,fValue2;
 88            var date1,date2,flag;
 89            f_checkDateEntry(obj1);
 90            if(DateIsValid==false)
 91            {
 92                return;
 93            }

 94            f_checkDateEntry(obj2);
 95            if(DateIsValid==false)
 96            {
 97                return;
 98            }

 99            //compare two date object
100            fValue1=dValue1.substring(dValue1.indexOf("-")+1)+"-"+dValue1.substring(0,dValue1.indexOf("-"));
101            fValue2=dValue2.substring(dValue2.indexOf("-")+1)+"-"+dValue2.substring(0,dValue2.indexOf("-"));
102            date1=new Date(fValue1);
103            date2=new Date(fValue2);
104            flag=date1-date2;
105            if(flag>=0)
106            {
107                alert("开始日期应该小于结束日期");
108                obj1.focus();
109                obj1.style.color="red";
110                obj1.select();
111                return;
112            }

113        }

抱歉!评论已关闭.