Skip to content Skip to sidebar Skip to footer

Regex For A Particular Date Format

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

Solution 1:

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

Solution 2:

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.

Solution 3:

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?

Solution 4:

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

functionValidateCustomDate(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 incorrectreturnfalse;
    }
    var day = parseInt(match[1], 10); // radix (10) is required otherwise you will get unexpected results for 08 and 09var 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 wayvar year = parseInt(match[3], 10);
    var date = newDate(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-yearreturn date.getDate() == day && date.getMonth() == month && date.getFullYear() == year;
}
console.log(ValidateCustomDate("1-Jan-2011"));  // trueconsole.log(ValidateCustomDate("01-Jan-2011")); // trueconsole.log(ValidateCustomDate("29-Feb-2011")); // falseconsole.log(ValidateCustomDate("29-Feb-2012")); // trueconsole.log(ValidateCustomDate("31-Mar-2011")); // trueconsole.log(ValidateCustomDate("31-Apr-2011")); // false

Post a Comment for "Regex For A Particular Date Format"