-1

Currently I trying to get a match given following conditions in a validation rule for Salesforce - If the number starts with any digits but 2 or 9, then the digit in the third position must be a 2 - The number must have exactly 9 digits - Repetitions of the same number are not allowed

This is the code I have, Is separated in three expresions because I'm not sure if I can have the same result in only one expression.

The Code I have copied is OK in syntax but is not matching correctly

OR( NOT(REGEX(Phone, "[0-9]{9}")),
REGEX(Phone, "([0-9])\\1{8}"),
REGEX(Phone, "\\b[0,1,3,4,5,6,7,8]{1}[0-9]{1}[0,1,3,4,5,6,7,8,9]{1}")
)
Ruben_CL
  • 1
  • 1

2 Answers2

1

This will get you everything accept for the repetition of numbers.

REGEX(Phone, "([01345678](?=\\d2)\\d{8})|([29]\\d{8})");

Some explanation of the the regex:

"()|()" the bar in the middle means 'OR' so the first parentheses 'OR' the second one

the (?=\d2) is a look ahead

if you combine that with the answer found here regex to find numbers with unique digits

you can make another regex statement

\d{3}(?!.*(.).*\1)\d{6}$ 

that way you can check and make sure there are no duplicates after the first 3 numbers since it appears that 202345678 would be a valid number

caleb baker
  • 144
  • 7
0

I don't know how you could do it in a single reg-ex, but you could have 2 and match either of them:

// If it starts with anything but a 2 or 9, 3rd digit must be a 2
[013456780][0-9]2[0-9]{6}  OR
// if it starts with a 2 or 9, rest of the digits can be anything
[29][0-9]{8}

Matching a single character is the default, so I left out the {1} as they are optional.

Blair
  • 76
  • 5