0

I want to have a regex for a date format, Example: 01-Jan-2011

I have written ^[0-9]{1,2}-[a-zA-Z]{3}-[0-9]{4} but does not check for all the valid dates like 50-AAA-2011 will also be considered as valid.

I am using Live Validation lib in javascript

My code

var sayDate = new LiveValidation('date');
    sayDate.add( Validate.Format, { pattern: /^[0-9]{1,2}-[a-zA-Z]{3}-[0-9]{4}/i });

Please help!

Salman A
  • 262,204
  • 82
  • 430
  • 521
AabinGunz
  • 12,109
  • 54
  • 146
  • 218
  • 2
    Validating that the format *looks* right is fine using regex, but using regex to validate the date is actually a valid one (what with leap-year, differing number of days per month etc) with regex is just wrong. – Jamiec Sep 23 '11 at 08:11
  • possible duplicate of [Regular Expression to match valid dates](http://stackoverflow.com/questions/51224/regular-expression-to-match-valid-dates) – Jamiec Sep 23 '11 at 08:14
  • How far do you want to take this? Do you want to catch leap years? What about other locales (`Oct` vs. `Okt` etc.)? – Tim Pietzcker Sep 23 '11 at 08:14
  • @Tim: Just valid Months and feb not having more than 28 – AabinGunz Sep 23 '11 at 08:18
  • @Abhishek: I've posted a solution that also checks for 28, 30, 31 days in month + 29 days in leap years – Salman A Sep 23 '11 at 09:32

4 Answers4

2

How about:

^(0?[1-9]|[1-2][0-9]|3[0-1])-(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)-[1-2][0-9]{3}$

This will still allow 31-feb-2011 so to check a date actually exists you'll probably need to use something other than a regex

buffcoredave
  • 179
  • 1
  • 9
1

Well clearly if you want only Jan-Dec to be validated, you need to specify it in the RegEx. And then you have to validate whether the date potion iscorrect. And then whether that date is actually valid for the given month or year and there are so many other cases. I believe RegEx is not the solution for this. Have you tried using Date.parse something like Datejs?

Ranhiru Jude Cooray
  • 19,542
  • 20
  • 83
  • 128
  • Can you give any snippet or example? – AabinGunz Sep 23 '11 at 08:18
  • I am sorry Abhishek, I haven't done any date parsing with JavaScript with either of the above mentioned methods. However, I would parse my date server side using `DateTime.Parse` if .NET and similarly in other languages. RegEx isn't the recommended approach I would take if not for a quick and dirty solution to check for a few basic date parsing rules. – Ranhiru Jude Cooray Sep 23 '11 at 08:22
1

Trying to validate a date format using regex is actually a lot more complex than it sounds. You can do basic validation quite easily, but there's always that bit more to do to get it perfect.

I would suggest instead using either a date picker control, which can do the validation for you (and also gives you the bonus of a nice shiny user interface feature), or else one of the existing Javascript libraries which can handle dates for you.

For date picker controls, there are any number of jQuery options. For date handling libraries, I would suggest looking up date.js.

Hope that helps.

Spudley
  • 166,037
  • 39
  • 233
  • 307
1

You can validate the syntax of the date via JavaScript regular expressions. You can check the semantics using the Date object, like so:

function ValidateCustomDate(d) {
    var match = /^(\d{1,2})-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(\d{4})$/.exec(d);
    if (!match) {
        // pattern matching failed hence the date is syntactically incorrect
        return false;
    }
    var day = parseInt(match[1], 10); // radix (10) is required otherwise you will get unexpected results for 08 and 09
    var month = {
        Jan: 0,
        Feb: 1,
        Mar: 2,
        Apr: 3,
        May: 4,
        Jun: 5,
        Jul: 6,
        Aug: 7,
        Sep: 8,
        Oct: 9,
        Nov: 10,
        Dec: 11
    }[match[2]]; // there must be a shorter, simpler and cleaner way
    var year = parseInt(match[3], 10);
    var date = new Date(year, month, day);
    // now, Date() will happily accept invalid values and convert them to valid ones
    // 31-Apr-2011 becomes 1-May-2011 automatically
    // therefore you should compare input day-month-year with generated day-month-year
    return date.getDate() == day && date.getMonth() == month && date.getFullYear() == year;
}
console.log(ValidateCustomDate("1-Jan-2011"));  // true
console.log(ValidateCustomDate("01-Jan-2011")); // true
console.log(ValidateCustomDate("29-Feb-2011")); // false
console.log(ValidateCustomDate("29-Feb-2012")); // true
console.log(ValidateCustomDate("31-Mar-2011")); // true
console.log(ValidateCustomDate("31-Apr-2011")); // false
Salman A
  • 262,204
  • 82
  • 430
  • 521