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!

1 Answer 1


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

    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).

  • Correct about the capturing groups and anchors. It only checks if the entire result matches, which has to end with end of string.
    – Adrian Larson
    Dec 19, 2021 at 19:32
  • 1
    If you want to allow just one apostrophe you would need to split it out from the square brackets. So instead it would be [a-zA-Z\\-\\.]+'?[a-zA-Z\\-\\.]+
    – Adrian Larson
    Dec 19, 2021 at 19:35
  • @AdrianLarson Thanks! for the example. I'm still learning about REGEX as I've only done simple number validations with it before. The example you provided looks like it could work, but I also need to allow for a space for First names that have 2 names (like Mary Anne). Is this OK: NOT(REGEX(FirstName, "[a-zA-Z \\-\\.]+'?[a-zA-Z \\-\\.]+")) I just added a space between the Z and the escape slash. It seems to work, but just wondering if you anticipate issues.
    – Heather
    Dec 20, 2021 at 15:57
  • 1
    The only issue there is you allow multiple spaces. If you want to limit to just one, you'll need an or grouping in the middle with the optional (?) quantity modifier. Strongly recommend you play around with regex101.com. There will be a few minor differences, but not too bad. Since you'll work with the raw expression, use only single backspaces. And on that site, wrap the entire expression in ^...$ so it has to match the whole string. Anyway if you want to test on that site, single space expression would look like: ^[a-zA-Z\-\.]+('| )?[a-zA-Z\-\.]+$.
    – Adrian Larson
    Dec 20, 2021 at 16:21
  • @AdrianLarson Thanks for the link to regex101 and for the help with the formula. I'll keep the multi space option for now, but give my PM the option to limit to just one space as I think one space should be sufficient.
    – Heather
    Dec 20, 2021 at 17:06

You must log in to answer this question.

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