Solved

RegularExpressionValidator (Date)

Posted on 2004-03-23
9
2,140 Views
Last Modified: 2007-12-19
Hi, I am looking for code that will set a regular expression validator to validate dates in this format: mm/dd/yyyy. I have this code already in: \d{2}/\d{2}/\d{4}

I need to have it accept (m or mm) and (d or dd). So if it was March 1st, it would be acceptable to write 3/1/2004 and not have to type out 03/01/2004. Minor i know, but thats what the boss wants.

Thanks for your responses.

Lance
0
Comment
Question by:Lance_Frisbee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 10

Expert Comment

by:eternal_21
ID: 10662157
What about \d{1, 2}/\d{1, 2}/\d{4} ?
0
 
LVL 2

Author Comment

by:Lance_Frisbee
ID: 10662202
Nope, that doesn't work.
0
 
LVL 2

Author Comment

by:Lance_Frisbee
ID: 10662204
It's probably something stupidly simple like that though, and i am just overlooking it.
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 10

Accepted Solution

by:
eternal_21 earned 50 total points
ID: 10662250
Sorry Lance, I typed the spaces by habit.  The regex expression should read: @"\d{1,2}/\d{1,2}/\d{4}"
0
 
LVL 2

Author Comment

by:Lance_Frisbee
ID: 10662281
Thank you eternal.
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 10662306
For completeness, I would use a method like this:

    bool IsValidDate(string dateString)
    {
      if(System.Text.RegularExpressions.Regex.Match(dateString, @"\d{1,2}/\d{1,2}/\d{4}").Success)
        try
        {
          DateTime.ParseExact(dateString, "dd/mm/yyyy", System.Globalization.CultureInfo.InvariantCulture);
          return true;
        }
        catch
        {
          return false;
        }
      else
        return false;
    }

Glad I could help, Lance.
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 10720292
There is an error in the above method!  Replace with:

### C#.NET ###

    bool IsValidDate(string dateString)
    {
      // Use Regex first to check for obvious non-matches
      // (eg: text, year in the wrong position).
      if(System.Text.RegularExpressions.Regex.Match(dateString, @"^\d{1,2}/\d{1,2}/\d{4}$").Success)
        try
        {
          // Then try to parse it into a DateTime to be sure
          // (makes sure that user cannot enter 99/01/2004).
          string[] allowedFormats;
          allowedFormats = new string[] {
                            "MM/dd/yyyy",
                            "M/dd/yyyy",
                            "MM/d/yyyy",
                            "M/d/yyyy"
                          };
          DateTime.ParseExact(dateString, allowedFormats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);          
          return true;
        }
        catch
        {
          return false;
        }
      else
        return false;
    }

###
0
 
LVL 2

Author Comment

by:Lance_Frisbee
ID: 10726798
Thank you eternal... i've already edited it :) i ended up with another validator. then parsing it to datetime

the validator ended up being: ([1-9]|1[012]||0[1-9]|1[012])[- /.]([1-9]|[12][0-9]|3[01]||0[1-9]|[12][0-9]|3[01])[- /.](20)\d\d

0
 

Expert Comment

by:BusyPhoto
ID: 10841452
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

759 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