Solved

RegularExpressionValidator (Date)

Posted on 2004-03-23
9
2,133 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

12 Experts available now in Live!

Get 1:1 Help Now