2

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"
);
Community
  • 1
  • 1
Awan
  • 18,096
  • 36
  • 89
  • 131
  • 3
    First you need to decide what the valid dates should be. For example, is `11/11/1111` valid? How about `01/01/0001`. How about `31/04/2010`? It's quite hard to validate dates using only regular expressions. The regexp above captures *most* of the problems and allows a relatively sane date into the system. If you need more robust validation, you should write something that actually tries to parse the date taking into account the semantics of date, month and year and decide whether it's valid or not. Regexps can only go so far. – Noufal Ibrahim May 11 '11 at 07:01
  • @Noufal Ibrahim: Yes you are right but for now I just want to invalid a date with all zeros (00/00/0000). Thanks for your concern.. – Awan May 11 '11 at 07:04
  • @Noufal Ibrahim is right, different decision is causing different solution – bungdito May 11 '11 at 07:05

3 Answers3

3

you can try:

function(value, element){
    return /^\d{1,2}\/\d{1,2}\/\d{4}$/.test(value) &&
        !isNaN(+new Date(value.split('/').reverse().join('/')));
}
  • I will use it instead of `return value.match(/^\d\d?\/\d\d?\/\d\d\d\d$/);` in function in my question ??? – Awan May 11 '11 at 07:02
  • Will `Date()` constructor accept the date in Australian format, `dd/mm/yyy` ? – alex May 11 '11 at 07:02
  • @cwolves Us damn Aussies :P BTW, I meant `yyyy` above :) – alex May 11 '11 at 07:04
  • Also, doesn't `+` invoke `getTime()`, and it returns `0` for an invalid time, or have I stuffed something up? [jsFiddle](http://jsfiddle.net/Eghc9/). – alex May 11 '11 at 07:07
  • @Awan - yes, this is in place of your function –  May 11 '11 at 07:07
  • @alex - you stuffed something up. You're doing `new Date(0.006030453791647822)` in that fiddle, thus zero. invalid dates return `NaN`. Try `'400/33/2010'` –  May 11 '11 at 07:09
  • @cwolves Wow, forgetting to quote a string is embarrassing! +1 for showing me the way :) – alex May 11 '11 at 07:11
  • Also, your regex is a little different in meaning to the OP's. Yours will only accept 2 digits for month and day, and the OP's will accept 1 or 2. – alex May 11 '11 at 07:15
3

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"
);
alex
  • 479,566
  • 201
  • 878
  • 984
  • Shouldn't it be `new Date(tokens[2], tokens[1], tokens[0])`? – James Khoury May 11 '11 at 07:02
  • @James `tokens[0]` contains the entire match. [jsFiddle](http://jsfiddle.net/9UEDy/). – alex May 11 '11 at 07:03
  • But you probably should subtract 1 from the month, as javascript counts from 0. – Shurdoof May 11 '11 at 07:08
  • @alex: I tried this. But it is not showing error message on `00/00/0000`. But it is showing error when I use wrong format for example `00/0/00`. Thanks for your reply.. – Awan May 11 '11 at 07:11
  • Pffft... My `.split('/').reverse().join('/')` is so much more elegant! :) –  May 11 '11 at 07:12
  • @cwolves Hehe, yeah, but I saw what happened last time you thought I copied you ;) – alex May 11 '11 at 07:13
  • hehehe, someone holds a grudge ;) –  May 11 '11 at 07:14
  • @alex: Oh I am confused. It is still accepting 00/00/0000 (Date with all zeros). It is showing error when I change the format but it is accepting correct format with all zeros digists(00/00/0000). I want to show same error when user use 00/00/0000. Or I am missing something ?? Anyway +1 for all for your efforts – Awan May 11 '11 at 07:24
  • @alex Whoops! I usually use `split()`. Didn't notice that you were using `match()` – James Khoury May 11 '11 at 23:27
2

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

function(value, element) {
        if (!value.match(/^00\/00\/0000$/)) return value;        
}
bungdito
  • 3,556
  • 4
  • 31
  • 38
  • This still won't tell you if the date is valid or not. In fact, it will be valid if the input is anything but `00/00/0000`, not so great for date validation. – alex May 11 '11 at 23:57