Solved

regular expression for date

Posted on 2003-11-12
12
901 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

705 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