Caroline
Caroline

Reputation: 969

regex - date format OR not specified/empty

I have a regex problem that I need some advice on. I am using a jquery plugin that validates input fields on a JSP. I am using the date validation. It all works fine but this field is not required and is not marked as required. When the user hits submit it shows a warning on the date input field since the empty string does not match the validation of a date. I need some way to say that nothing or a date is valid in a regex.

Here is the regex for the date; is there any way to put "or empty" into this also:

"date":{
           "regex":"/^[0-9]{1,2}\-\[0-9]{1,2}\-\[0-9]{4}$/",
           "alertText":"* Invalid date, must be in MM-DD-YYYY format"},

Thanks in advance

Upvotes: 0

Views: 7617

Answers (2)

Robert K
Robert K

Reputation: 30328

Try:

/^([0-9]{1,2}\-\[0-9]{1,2}\-\[0-9]{4})?$/

It will then match when a valid date is supplied, or the field is left blank.

Upvotes: 1

ennuikiller
ennuikiller

Reputation: 46985

(^[0-9]{1,2}-[0-9]{1,2}-[0-9]{4}$|^$)

Upvotes: 1

Related Questions