Datetime regExp which doesn't allow empty value

Posted on 2007-10-01
Last Modified: 2010-05-18
How should I modify this Datetime regular expression to check also that empty value is NOT legal (including whitespace chars)?

^([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])[- /.][0-9]{4}$
Question by:j_lainio
    LVL 14

    Accepted Solution

    it is better not to mingle DATE CHECKING with EMPTY string checking...
    if it is a empty string checking then this regex will do..becos empty string is not a date....
    LVL 84

    Assisted Solution

    An empty string does not match that regular expression, so what do you need to modify?

    Author Comment

    I fully understand your point and agree with it! My case is that i'm using Asp.Net RegularExpressionValidator control to validate user input. Input field must have value which means that empty value can not given. This control does validation with my regular expression but will pass empty value - which I don't want.

    And actually I found a way (a bit stupid, but anyway) to handle problem with these controls. I will use 2 different controls to handle my validation like this:

    Validate that value is NOT empty:
    <asp:RequiredFieldValidator ControlToValidate="DateTimeTextBox" Text="Cannot be empty" runat="server" Display="Dynamic" />

    Validate that value is in correct format:
    <asp:RegularExpressionValidator ControlToValidate="DateTimeTextBox" Display="Dynamic" Text="Format is invalid..." runat="server" ValidationExpression="^([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])[- /.][0-9]{4}$" />

    Author Comment

    I have to mark my own findings as an answer for my problem... sorry! ;)
    LVL 63

    Expert Comment

    Sorry but you are here exchanging with other experts findings. And you cannot keep your points because you stated wrong your question and expected wizards can read your mind and your dot.Net environment. Both Experts answered absolutely correct your question so is the points split the only choice. But I am also sure that both Experts grant the points back as a gift to you ;-)

    And I am also sure that both Experts would not argue with you for points because they are too polite, therefore I am jumping in to help you getting help from them in your next question with your saved points :)

    Can you see it that way?

    Here also an internet page stating the ASP.NET RegExp problem:
    .. you'll quickly discover that you can submit the form without entering any text into the text box. The RegularExpressionValidator control does not require a value.
    The only way to require a value is to combine RegularExpressionValidator with RequiredFieldValidator. Nothing prevents you from associating multiple Validator controls with the same control.
    LVL 14

    Expert Comment

    i think the points has to be split and the question must not be abandonned...
    LVL 63

    Expert Comment

    Thanks j! :)

    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

    Suggested Solutions

    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…

    755 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

    18 Experts available now in Live!

    Get 1:1 Help Now