Link to home
Start Free TrialLog in
Avatar of GessWurker
GessWurker

asked on

Need regular expression for mm/dd/yyyy date possibly preceded by bullet char

I need a regular expression to test for mm/dd/yyyy dates at the beginning of lines in a form field, as in the example below:
·07/29/2019 event 3
·07/08/2019-event 2
·05/29/2019: event

Users can enter as many dates and details as they want, but I need to verify the date format.

Users may also enter only one date, as below.

07/29/2019 event

Here's what I have (that doesn't work):

var dateString = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/

But I don't know how to test (or ignore) the bullet character. I've tried .  I've tried \·  

But none of my efforts find the lines below valid (though I want them to be accepted):

·07/29/2019 event 3
·07/08/2019-event 2
·05/29/2019: event

Here's the context in my javascript:  else if(!dateString.test(theForm.EI16.value)) {alert("Please use mm/dd/yyyy format for all dates.");theForm.EI16.focus();return false;}

Who can help?
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

^·?\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}

I believe you just need to use the ? to indicate the bullet may or may not be there.  For the rest, you have $ which means the date is at end of end so you have to add in that there will be other characters or just remove $.
Firstly, the $ at the end of your pattern matches the end of the string. ie it will cause the pattern to only match data where the $ is the end of the string, such that strings with text after the date don't match.
Ha, should have refreshed my page...
Maybe this?

(?<=\·)\d{1,2}(\/|\-|\.)\d{1,2}\1\d{4}
Avatar of GessWurker
GessWurker

ASKER

Unfortunately, ^·?\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}  just allowed this to pass:  

·07/29/2019 event 3
·07/8/2019-event 2   //<--this is a problem
·05/29/2019: event

But I need all the dates to be mm/dd/yyyy
And this one -- (?<=\·)\d{1,2}(\/|\-|\.)\d{1,2}\1\d{4} -- allowed this to pass:


·07/29/2019 event 3
·7/8/2019-event 2   //<--this is a problem,   I need to force 07/08/2019  with a line like this; no single digit months or days allowed
·05/29/2019: event
Kevin's suggestion should work. I suggest also putting the date delimiter characters into square brackets though, like this for easier reading:
var dateString = /^·\d{1,2}([\-\/.])\d{1,2}\1\d{4}/;

Open in new window


Tested here:
https://jsfiddle.net/gh6v34xn/4/
In that case, this should force the days and months to contain 2 digits

(?<=\·)\d{2}(\/|\-|\.)\d{2}\1\d{4}
For disallowing single days and months try:
var dateString = /^·\d{2}([\-\/.])\d{2}\1\d{4}/;

Open in new window

Subodh, the issue with your solution is that it will only be true if the date is preceeded by a bullet - the bullet is optional.

This should be a working regex forcing 2 digit dates:
var dateString = /^·?\d{2}([\-\/.])\d{2}\1\d{4}/;

Open in new window

If the form field value contains mulitple lines, but at least one line doesn't contain a valid date, then is the value valid?
@wilcoxon
Yes, you are correct . That pattern will match a date only when it is preceeded by a bullet.
@Terry Woods:  No. If multiple lines are present, each of the lines must begin with mm/dd/yyyy (preceded by the bullet char)
Folks. So far, it seems all of the suggestions only care about the first entry. The second and third entries slip by with single digit months & days. Will hope to wake up to a solution. If not, tomorrow is another day! Cheers.
/^·?\d\d\/\d\d\/\d{4}/

User generated image
So far, it seems all of the suggestions only care about the first entry. The second and third entries slip by with single digit months & days.
That shouldn't be the case if you are properly testing the string using the regex.test after setting the global flag
If dateString contains all of the dates at once, you are correct - none of the solutions presented will check all of them.

This should match based on all of your criteria - 2 or more dates preceeded by bullet or 1 date without bullet, optional extra text after date, and month/day exactly 2 digits each.
var dateString = /\A(?:^·\d{2}([\-\/.])\d{2}\1\d{4}.*\n?){2,}\Z|\A\d{2}([\-\/.])\d{2}\1\d{4}.*\n?\Z/m;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To be honest though, it may have been sensible just to split the string by lines, then validate each line... complex regex patterns can become a headache.
Folks - We've got a winner! I tested every regx you came up with and only one worked under all the circumstances/scenarios I threw at it. Terry Woods did the work. Thanks, Terry!

Kudos to Terry for the regx below:

var dateString = /^(?:·?\d{2}([\-\/.])\d{2}\1\d{4}[^\n]*(?:\n+|$))+$/;

Open in new window

@Terry: Regarding splitting and evaluating lines one by one...  I had the same notion this morning. But then, when I couldn't break your last suggestion, I put the notion away.  : )