开发者

How to invalid 00/00/0000 date in JS

I am using jQuery Validation Plugin. I used this post to validate my dates using this plugin. It is working but there is a problem. It is accepting 00/00/0000 date as well. How can I invalid this date as well by modifying following function.

$.validator.addMethod(
    "australianDate",
    function(value, element) {
        return value.match(/^\d\d?\/\d\d?\/\d\d\d\d$/);
    },
    "Please enter a date in the format 开发者_开发技巧dd/mm/yyyy"
);


This will ensure the data is in correct format and is also a valid date.

$.validator.addMethod(
    "australianDate",
    function(value, element) {
        var tokens =  value.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})/);

        if (tokens == null) {
            return false;
        }

        var date = new Date(tokens[3], --tokens[2], tokens[1]);

        return ! isNaN(date.getTime());

    },
    "Please enter a valid date in the format dd/mm/yyyy"
);


you can try:

function(value, element){
    return /^\d{1,2}\/\d{1,2}\/\d{4}$/.test(value) &&
        !isNaN(+new Date(value.split('/').reverse().join('/')));
}


maybe you can try this, perhaps it can works :)

function(value, element) {
        if (!value.match(/^00\/00\/0000$/)) return value;        
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜