Regular expression for a date. This is for ASP .NET regular expression validator

Posted on 2009-04-28
Last Modified: 2012-05-06
I need a regular expression for a date. The date can be in one of the three formats
Where the user must enter four digits for the year. "mm" and "dd" are numeric and are mandatory.

mm dd yyyy
Question by:brgdotnet
    LVL 40

    Accepted Solution

    You can try it in one fell swoop if you want the capturing groups to always be $1, $2, and $3:

    ^([01]\d)[-\/ ]([0-3]\d)[-\/ ](\d{4})$

    The only thing is this regex will allow different separator characters to be mixed in one date.


    To check that, you could break it out into individual regexes or you can capture the separators. If you aren't worried about capturing, then put alternations:

    ^(([01]\d)-([0-3]\d)-(\d{4})|([01]\d)\/([0-3]\d)\/(\d{4})|([01]\d) ([0-3]\d) (\d{4}))$

    In this case, $1 will capture the whole date, but mm, dd and yyyy may be captured in any 3 of the 9 alternate groups.

    This regex will filter out a few invalid dates, like months that start with something other than 0 or 1 in the 1st digit, and so forth, but it is not a full date checker.
    LVL 40

    Expert Comment

    Or if you really don't care about checking valid digit ranges or capturing, then you can simplify to use \d for all digits and remove parens.

    ^\d\d[-\/ ]\d\d[-\/ ]\d\d\d\d$


    ^(\d\d-\d\d-\d\d\d\d|\d\d\/\d\d\/\d\d\d\d|\d\d \d\d \d\d\d\d)$
    LVL 3

    Assisted Solution

    Check out this site - for all futures Regex's as its got it all. Much easier than having to fight for expressions. Cheers

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
    As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now