Regex Validation Rule Help

formularegular-expressionsvalidation-rule

Looking for help on correcting my validation rule on a First Name field. We want to allow A-Z, a-z, and then also allow apostrophe, hyphen, and period ('-.)

When testing my formula below, it allows a period, but it also seems to allow other characters as well. I know the period is a special character so I'm thinking I'm just representing it wrong.

NOT(REGEX(FirstName, "([a-zA-Z'-\.]+$)"))

Also, if I can get the period thing to work another ideal solution would be to ONLY allow one instance of an apostrophe (we are trying to prevent users from putting names in brackets or quotes), so any suggestions on that would be greatly appreciated too!

Thank you!

Best Answer

Normal regex uses a single backslash to escape a character, but in Salesforce, we need to double each backslash.

You also need to escape the hyphen (-) after the apostrophe (because it has a special meaning inside of square braces).

So I believe you're looking for NOT(REGEX(FirstName, "[a-zA-Z'\\-\\.]+")). I don't think that making the overall regex a capturing group does anything for you here (which is what the ( and ) enclosing the entire regex does). I don't think the ending anchor $ does anything for you either.

As for the "only one apostrophe" part, I'd think that'd be easiest to achieve with a separate regex, so your validation rule would end up looking like

AND(
    NOT(REGEX(<first one>)),
    NOT(REGEX(<second one>))
)

Regex is only capable of some pretty basic/simple counting, but something like '.*' ought to do (apostrophe followed by 0 or more other characters, followed by an apostrophe).

Related Topic