6

What

I'm trying to do a validation rule to count number of words in a text field and return an error if field have more than 80 words.

Validation Rule Condition Formula

NOT(REGEX(TRIM( AC_Summary__c),'^(\s\W*){0,80}$))

Error

Error: Validation Formula "Words_AC_Summary" Invalid (Regular expression runtime exceeded for: (\s*\W*){0,80})

Questions

Is there any way to reduce the runtime?

Does someone know how to validate data without using a regex?

2

1 Answer 1

5

Did you mean \w*? Currently, your regex is returning a pattern (true) if it finds whitespace then a nonword character...your use case is perfectly reasonable for regex and if it's exceeding runtime caps then it's likely your expression.

I tested this and it works fine, even with multiple spaces:

NOT(REGEX(TRIM(Name),'^(\\S+\\s+){0,79}\\S+'))

We want the entire expression to evaluate to true (to throw the error) - so for NOT(REGEX(A,B)) we want string A to match expression B if A is a valid input.

Patterns:

TRIM(Name) -> eliminate initial and ending whitespace from text area string
\S+        -> finds a 'word,' defined here by any character except whitespace, one or more times 
\s+        -> followed by one or more whitespace characters
{0,79}     -> repeat this 0 to 79 times
\S+        -> include the last word

For another interval of n to m words inclusive, you would just make the repeater {n-1, m-1}... e.g. if you wanted 3 to 5 words, it would be NOT(REGEX(TRIM(Name),'^(\\S+\\s+){2,4}\\S+'))

2
  • Brian, this works in all cases except if we put something like 'word , word'. It takes as 3 words. I have changed the regular expression to NOT(REGEX(TRIM(AC_Summary__c),'^(\\S+\\W*\\s+){0,79}\\S+')) and works fine. Thank you very much for your help. May 21, 2015 at 7:39
  • Does this count \n \r and \t?
    – C0DEPirate
    Jul 2, 2020 at 5:19

You must log in to answer this question.

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