Solved

RegularExpressionValidator (Date)

Posted on 2004-03-23
9
2,137 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

3 Use Cases for Connected Systems

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

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

810 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