Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

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

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
rtay
Asked:
rtay
  • 3
1 Solution
 
sjklein42Commented:
(^$)|(^(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
 
sjklein42Commented:
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
 
rtayAuthor Commented:
Was this regex supposed to allow the field to be null as well as validate the date?
0
 
sjklein42Commented:
Yes.  It accepts either an empty string or a valid date.
0
 
käµfm³d 👽Commented:
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now