Solved

RegularExpressionValidator (Date)

Posted on 2004-03-23
9
2,139 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
  • 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 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