Solved

regular expression for date

Posted on 2003-11-12
12
898 Views
Last Modified: 2008-02-01
Hi,

              Could anyone  provide me a regular expression for  date in MMDDYYYY format?....

thank you
0
Comment
Question by:sunny1510
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 48

Expert Comment

by:Mikal613
Comment Utility
Can you explain please?
0
 

Author Comment

by:sunny1510
Comment Utility
I am validating a date entered in a text box.....The user is only allowed to enter the date in mmddyyyy format......i know checking the format is easy with regex .......but i want the regular expression to check if it is a valid date.....
0
 
LVL 48

Expert Comment

by:Mikal613
Comment Utility
Well if in your Glabal Region Settings if your Setting is set to mmddyyyy then just use IsDate()
0
 

Author Comment

by:sunny1510
Comment Utility
Thanks for the response  Milka..The date is being validated when the textbox loses focus.....for certain date fields i also allow default value of 00000000 or a blank field. In that case this isdate() will give me an error. That is y i thought i will use regex.
0
 
LVL 48

Expert Comment

by:Mikal613
Comment Utility
so

If txtDAte.text = "" or txtDAte.text  = "00000000"  then
else
    if not isdate(txtdate.text) then Msgbox "Wrong Date"
end if

 
0
 
LVL 44

Expert Comment

by:Arthur_Wood
Comment Utility
rahter than requiring the user to enter the date as a sequence of digits (by the way, since the entry IS a sequence of digits, a regex will not help, since any digit is valid, but you want to be sure that they are no just digits, but also make up a valid date, and that is NOT what regex is used for), why do you not prenet the user with a Calendar control, and they you are CERTAIN they have picked a valid date?

AW
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 28

Expert Comment

by:iboutchkine
Comment Utility
Here is an example. I think you will find it helpful

 ' The following pattern checks whether the input string is a date in the
        ' format mm-dd-yy or mm-dd-yyyy. Key pattern elements used are:
        '
        '   \d{1,2}     Month and day numbers can have 1 or 2 digits. The use of
        '               (\d{4}|\d{2}) means the year can have 2 or 4 digits.
        '   (/|-)       Either the slash or the dash are valid date separators.
        '   \1          The separator used for the day and year must be the same
        '               as the separator used for month and day. The 1 refers to the
        '               first numbered group, defined by parentheses, e.g, (/|-).
        '  
        ' You could improve on this pattern by ensuring that digits do not start with
        ' a zero and that they are in a valid numerical range.
        '
        If Not Regex.IsMatch(txtDate.Text, _
            "^\s*\d{1,2}(/|-)\d{1,2}\1(\d{4}|\d{2})\s*$") Then

            txtDate.ForeColor = Color.Red
            IsValid = False
        Else
            txtDate.ForeColor = Color.Black
        End If
0
 

Author Comment

by:sunny1510
Comment Utility
I have a number of text boxes in my app.....what if there is  a situation where one of these will accepts a value of "" and the other other textbox  needs a valid date .... In that case if the user enters a "" and exits... the isdate() fuction will accept it.
0
 

Author Comment

by:sunny1510
Comment Utility
Hi all,

       Here i found a link where they have used regular expression for validating dates of mm/ddy/yyyy format.I am trying to use this regex since this appliction is a data entry program and i would prefer the user keying it in a text box.....Also I cannot write one function which will handle all the date validations since every date-text box has a  different default value......

http://www.regxlib.com/REDetails.aspx?regexp_id=279

please let me know if there is any other way of doing this...... thanks
0
 
LVL 48

Expert Comment

by:Mikal613
Comment Utility
why dont you use a masked Edit control

you can set your MASK?
0
 
LVL 9

Expert Comment

by:malharone
Comment Utility
i'll recommend what i've always recommended for regex.
great for building & testing
http://www.codeproject.com/dotnet/expresso.asp?target=expresso
(?<Month>\d{1,2})/(?<Day>\d{1,2})/(?<Year>(?:\d{4}|\d{2}))(?x)

it includes a predefined regex for data.
however it'll NOT check for proper 'date value' given the month number.
so you could enter 4/31/2001 and it'll accept it.
So what you'd need to do is call
dim yourdatestring as string="4/31/2001"
if isDate(yourDateString)=false then
  messagebox.show(yourdateString & " is not a valid date")
else
  'all is OK
end if
0
 
LVL 9

Accepted Solution

by:
malharone earned 70 total points
Comment Utility
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

9 Experts available now in Live!

Get 1:1 Help Now