I'm working on a validation that would require a user to enter only numbers that have to be exactly 9 numbers and be separated by lines. The below is working for that, but is it possible with straight RegEx to add criteria prevent a duplicate of any number set?

!REGEX(SomeCustomField__c, '^[0-9]{9}+(\r\n[0-9]{9}+)*$')

If there way you can prevent a number from appearing twice without Apex?

// Prevent duplicate of any number already entered

1 Answer 1


Yes, regex can do this.

NOT(REGEX(SomeCustomField__c, '^(?s)\\d{9}(\\s+\\d{9})$')) ||
REGEX(SomeCustomField__c, '(?s)(\\d{9}).*\\1')

The trick here is that we go in to "single line mode" (?s) to allow . to match newlines, then we check each nine-digit string (\\d{9}) with any intervening characters (.*), followed by the same group again (\\1).

  • I should have thought of using an AND expression. Having a bit of an issue as it doesn't seem to work in the same validation rule from what I can see. If I separate them into different validation rules it works as intended. Let me double-check a few things.
    – Andrew L.
    Feb 6, 2021 at 19:38
  • @AndrewL. I actually meant OR. Silly mistake, really.
    – sfdcfox
    Feb 6, 2021 at 20:29
  • No problem. It gave me an opportunity to work with REGEX and validation. Thanks again.
    – Andrew L.
    Feb 6, 2021 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.