Solved

regular expression for date

Posted on 2003-11-12
12
899 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
ID: 9731596
Can you explain please?
0
 

Author Comment

by:sunny1510
ID: 9731655
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
ID: 9731720
Well if in your Glabal Region Settings if your Setting is set to mmddyyyy then just use IsDate()
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:sunny1510
ID: 9731770
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
ID: 9731792
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
ID: 9731820
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
 
LVL 28

Expert Comment

by:iboutchkine
ID: 9731837
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
ID: 9731877
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
ID: 9731961
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
ID: 9732154
why dont you use a masked Edit control

you can set your MASK?
0
 
LVL 9

Expert Comment

by:malharone
ID: 9733127
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
ID: 9733148
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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