博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js实现日期验证
阅读量:5277 次
发布时间:2019-06-14

本文共 2353 字,大约阅读时间需要 7 分钟。

直接上代码:

function dateValidate(date, dateformat, separator) {

            if (date == null || date == "") return false;

            var date_part_index = datePartIndex(dateformat, separator);
            var date_array = date.split(separator);
            var yearstr = date_array[date_part_index.y];
            var monthstr = date_array[date_part_index.m];
            var daystr = date_array[date_part_index.d];

            if (isNaN(yearstr)) return false;

            if (isNaN(monthstr)) return false;
            if (isNaN(daystr)) return false;

            var year = parseInt(yearstr);

            var month = parseInt(monthstr)-1;
            var day = parseInt(daystr);
            var date = new Date(year, month, day);
            var y = date.getFullYear();
            var m = date.getMonth();
            var d = date.getDate();
            if (y != year || m != month || d != day) {
                return false;
            }
            return true;
        }

function datePartIndex(dateformat, separator) {

            var partIndex = {};
            var part_array = dateformat.split(separator);
            for (var i = 0; i < part_array.length; i++) {
                switch (part_array[i]) {
                    case "YYYY":
                        partIndex.y = i;
                        break;
                    case "MM":
                        partIndex.m = i;
                        break;
                    case "dd":
                        partIndex.d = i;
                        break;
                }
            }
            return partIndex;

}

dateValidate 是实现js日期验证的方法

datePartIndex 是实现js日期验证的辅助方法

 

下面日期证验的一些测试用例

alert(dateValidate("2005-10-21","YYYY-MM-dd","-") == true);

alert(dateValidate("2005-10-213","YYYY-MM-dd","-") == false);
alert(dateValidate("200a5-10-10","YYYY-MM-dd","-") == false);
alert(dateValidate("a2005-10-10","YYYY-MM-dd","-") == false);
alert(dateValidate("2005a-10-10","YYYY-MM-dd","-") == false);
alert(dateValidate("02005-1-1","YYYY-MM-dd","-") == true);
alert(dateValidate("2005-12-32","YYYY-MM-dd","-") == false);
alert(dateValidate("2005-13-3","YYYY-MM-dd","-") == false);
alert(dateValidate("2005-12-12","YYYY-MM-dd","-") == true);
alert(dateValidate("2005-2-30","YYYY-MM-dd","-") == false);
alert(dateValidate("2005-(-12)-3","YYYY-MM-dd","-") == false);
alert(dateValidate("2005-(12)-3","YYYY-MM-dd","-") == false);
alert(dateValidate("0-0-0","YYYY-MM-dd","-") == false);
alert(dateValidate("1-1-1","YYYY-MM-dd","-") == false);
alert(dateValidate("2005-2-30","YYYY-MM-dd","-") == false); 
alert(dateValidate("2012-2-30","YYYY-MM-dd","-") == false);
alert(dateValidate("2012-2-29","YYYY-MM-dd","-") == true);
alert(dateValidate("2012/2/29", "YYYY/MM/dd", "/") == true);
alert(dateValidate("2/29/2012", "MM/dd/YYYY", "/") == true);

以上测试用例的返回结果都应该是true,就正确。

转载于:https://www.cnblogs.com/itprogrammer/archive/2012/11/28/2792889.html

你可能感兴趣的文章
【翻译自mos文章】当点击完 finishbutton后,dbca 或者dbua hang住
查看>>
Linux编程简介——gcc
查看>>
2019年春季学期第四周作业
查看>>
MVC4.0 利用IActionFilter实现简单的后台操作日志功能
查看>>
rotate the clock
查看>>
bugku 变量
查看>>
数据库01 /Mysql初识以及基本命令操作
查看>>
数据库02 /MySQL基础数据类型以及多表之间建立联系
查看>>
Python并发编程04/多线程
查看>>
CF461B Appleman and Tree
查看>>
CF219D Choosing Capital for Treeland
查看>>
杂七杂八的小笔记本
查看>>
51Nod1353 树
查看>>
CF1215E Marbles
查看>>
BZOJ2339 HNOI2011卡农(动态规划+组合数学)
查看>>
octave基本操作
查看>>
axure学习点
查看>>
WPF文本框只允许输入数字[转]
查看>>
dom4j 通用解析器,解析成List<Map<String,Object>>
查看>>
第一个项目--用bootstrap实现美工设计的首页
查看>>