Solved

Regulare Expression Validator for date that also allows field to remain blank

Posted on 2011-02-14
5
353 Views
Last Modified: 2012-05-11
I have an optional date field.  This field needs to be checked for proper date mm/dd/yyyy or if blank allow the null.  This feild can have a date in it, and then the user will null the date out if no longer needed.  The validator I use always checks for the date and so does not allow the null.  

Thanks for the help
0
Comment
Question by:rtay
  • 3
5 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 34890935
(^$)|(^(0[1-9]|[12][0-9]|3[01])\-(0[1-9]|1[0-2])\-([12][0-9][0-9][0-9])$)

Open in new window

0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
ID: 34890950
Prior solution was for hyphenated dates.  This one works for slash-separated dates:

(^$)|(^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[0-2])\/([12][0-9][0-9][0-9])$)

Open in new window

0
 
LVL 5

Author Comment

by:rtay
ID: 34891559
Was this regex supposed to allow the field to be null as well as validate the date?
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34891589
Yes.  It accepts either an empty string or a valid date.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34891624
A slightly shorter version  : )
^(?:(?:0[1-9]|[12]\d|3[01])/(?:0[1-9]|1[012])/(?:[12]\d{3}))?$

Open in new window

0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

809 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