0

I would like to set up a validation rule to check that there is at least two digits in the string. in Python I would go with this

NOT(REGEX(Field_Name__c, "(?=^(?:\D*\d\D*){2,}$)"))

But Salesforce seem to refuse this input for regex.

Does someone know how I would verify this.

Correct version: AAAA22AAAAA (at least 2 digits) Wrong version: AAAAAAAAAA (no digits

2
  • Your expression would not work. You need the {2,} after \d.
    – Adrian Larson
    Jun 10, 2020 at 16:12
  • Do you mean like this? (?=^(?:\D*\d{2,}\D*)$) ?
    – RomeoRomeo
    Jun 11, 2020 at 9:21

2 Answers 2

0

The REGEX function in Salesforce formulas needs to have \ characters escaped (documentation).

I believe a straightforward formula like this should work:

NOT(REGEX(\\d{2,}))

This will check that there are at least two digits in sequence within the string. Your original regex may have some problems. I could not get it to match your expected input on regexr.com. I'm also making the assumption that the other characters do not matter, because your original pattern contains \D* which is 0 or more non-digit characters.

1
  • Sorry I forgot to mention it doesn't have to match characters consecutively, but a better exemple might be AAAAA1AAA1 or AAAAA2BBB3
    – RomeoRomeo
    Jun 11, 2020 at 9:23
0

I eventually went with NOT(REGEX(Custom_Field__c, "^(?=(?:\\D*\\d){2})[a-zA-Z0-9]*$"))

Thank you

You must log in to answer this question.

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