0

I'm trying to find a regex pattern for a validation rule on an opportunity that will allow dd/mm/yyyy one or more lines in a long text field named Dates_offered__c.

For example, I want to allow:
dd/mm/yyyy

or

dd/mm/yyyy
dd/mm/yyyy

but not allow
dd/mm/yyyy dd/mm/yyyy

also I do not want to allow any letter characters, it should be numbers and "/" only.

This means the text field should always have this format
31/10/2019
10/05/2021
12/09/2021

At the moment I have this but it doesn't work:

not(REGEX( Dates_offered__c , "(0[1-9]|[12][0-9]|3[01])[/](0[1-9]|1[012])[/]\\d\\d\\d\\d \r\n"))
2
  • Hi. Please could you clarify in what context you are using this. Is this in a validation rule?
    – Phil W
    Oct 17, 2019 at 10:13
  • It is in a validation rule, yes
    – Joe Hayes
    Oct 17, 2019 at 10:21

2 Answers 2

3

It's a rather complex Regex but here's one that works:

((?!00)[0-3][0-9]\/(?!00)[0-1][0-9]\/(?!0000)[0-9]{4}(?![0-9])\R?)+

Here's some sample data:

07/12/1985
30/01/1986
12/07/2016
01/09/2019

You can try it out regex101.com.

And here's how to test that in apex:

Boolean matches = Pattern.matches('((?!00)[0-3][0-9]\\/(?!00)[0-1][0-9]\\/(?!0000)[0-9]{4}(?![0-9])\\R?)+', '07/12/1985\n30/01/198630/01/1986');
system.debug(matches);
0
0

As per the documentation, REGEX applies Java's regex handling.

This means you can use "\n" (actually done using "\\n" as per the documentation) to match new line.

Try something like (simplified):

"([0-9]{1,2}/[0-9]{1,2}/[0-9]{4}\\n)*[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}(\\n)?"

This should allow zero or more dates with new lines followed by a single date followed by an optional newline.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .