4

I'm looking for some RegEx for a custom pattern validation for a date field in InfoPath 2010. The accepted date format is m/d/yyyy or mm/dd/yyyy.

Attempt 1: (\d{1,2})/(\d{1,2})/(\d{4})

Attempt 2: (0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\d\d)

Had better luck with attempt 1, and not much at all with attempt 2.

Jonah Bishop
  • 11,619
  • 4
  • 38
  • 68
manh2244
  • 133
  • 2
  • 15
  • So if attempt 1 worked, what's the problem? – Jonah Bishop Apr 11 '13 at 18:24
  • if you're looking to validate the date to be an existing one, just a regex is not what you're looking for. – djjeck Apr 11 '13 at 18:26
  • Received a validation error when I entered 1/1/2001. The current date worked, though. Not sure why 1/1/2001 failed. – manh2244 Apr 11 '13 at 18:27
  • To clarify, this is a "Date Available" field and will be used for future dates. – manh2244 Apr 11 '13 at 18:28
  • 4
    Using a regex for actual dates will probably be too cumbersome. Remember, not all months have days 29-31, so you'd need a gigantic regular expression to cover any real possibility. – Guillermo René Ramírez Apr 11 '13 at 18:38
  • Is `m/dd/yyyy` valid? – Walls Apr 11 '13 at 18:39
  • What if I only worry about the format and not whether the date is actually valid? I just need something that will ensure that only d/d/dddd or dd/dd/dddd formats are entered. – manh2244 Apr 11 '13 at 18:41
  • m/dd/yyyy would be valid, so long as this wasn't used for a single digit day. – manh2244 Apr 11 '13 at 18:42
  • If you set the format of the date field, then whatever the user types in will be coerced into the stated format (if possible) or show an error. No regex needed. Is this a possibility? – ktharsis Apr 12 '13 at 00:17
  • Since the form was done using InfoPath 2010, I set the date field to the date format there. Does your suggestion apply to within InfoPath, are are you suggesting I implement this setting elsewhere? I should also note that this website sits on the SharePoint 2010 platform. – manh2244 Apr 12 '13 at 14:39

3 Answers3

4

I've been having some date and time validation issues with InfoPath 2010 and regex pattern matching can be a useful approach. A basic regex for validating m/d/yyyy (without catering for the specific days in a month and allowing for '0' prefix to month or day) would be something like the following (untested):

(0?[1-9]|1[012])\/(0?[1-9]|[12][0-9]|3[01])\/\d{4}

For something more sophisticated you could have a look at this SO answer.

However, in InfoPath the format of the date displayed can be completely different to the internal format and it is this internal format that your regex needs to match. If you drop a calculated field on your form and set it to the date field you want to validate you will see something like:

2013-05-08T12:13:14

So a regular expression (again ignoring specific days per month) required to validate the date component of this is:

\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])

But this won't match against the example date because it doesn't account for the time portion following the "T". So the trick is to use an expression to perform the match against the date substring only, e.g. in my case the following works:

not(xdUtil:Match(substring-before(dfs:dataFields/my:SharePointListItem_RW/my:DateCreated, "T"), "\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])"))
Community
  • 1
  • 1
David Clarke
  • 12,002
  • 8
  • 80
  • 105
0

I tried the following and it worked:

 \d{4}-\d{1,2}-\d{1,2}

As David pointed out the internal format might be different than the one displayed because when I tried \d\d/\d\d/\d\d\d\d it didn't work even though it caters to the displayed format of the date.

Massimiliano Kraus
  • 3,214
  • 5
  • 20
  • 40
0

I had the same problem.

I used a rule on the date field to set another hidden text field to

string(datefield).

That always came out YYYY-MM-DD which is not too hard to create a regex against. I used this one.

((19|20)\d\d)-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])

Remember that it has to be an XML Regex which has some restrictions.

Then I set another rule on the hidden field to set a Boolean IsDateValid.