Solved

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

Posted on 2011-02-14
5
354 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

791 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