Salesforce REGEX Validation Rule to Reject Emojis

Junji Zhi
1 min readJun 18, 2020

--

For a text field, we can’t allow users to enter emojis in the text because that field is later ingested in a MySQL database doesn’t have utf8mb4 support yet.

As a result, we need to add a validation rule to detect emoji and sends an error message.

I ended up using a whitelist approach based on the unicode categories:

(\p{L}|\p{Sc}|\p{M}|\p{Z}|\p{N}|\p{P}|[\n\r])+

With this, you can compose your own validation rules.

Because Salesforce REGEX formula requires escaping backslashes and other quirks, it’s tricky to compose the regex for emoji. For example, the validation file looks like:

<ValidationRule>
<fullName>My_Field_Validation</fullName>
<active>true</active>
<description>My_Field validation rule</description>
<errorConditionFormula>AND(
LEN (My_Field__c) &gt; 0,
NOT(REGEX(My_Field__c, &quot;(\\p{L}|\\p{M}|\\p{Sc}|\\p{Z}|\\p{N}|\\p{P}|[\\n\\r])+&quot;))
)
</errorConditionFormula>
<errorDisplayField>My_Field__c</errorDisplayField>
<errorMessage>Special characters are not supported</errorMessage>
</ValidationRule>

Update:

I found the emoji source from here. To translate it into Salesforce / Java 6 Syntax, the regex looks like below

REGEX(My_Field, &quot;[\\s\\S]*(\\u00a9|\\u00ae|[\\u2000-\\u3300]|[\\ud83c\\ud000-\\ud83c\\udfff]|[\\ud83d\\ud000-\\ud83d\\udfff]|[\\ud83e\\ud000-\\ud83e\\udfff])[\\s\\S]*&quot;)

Explanations:

  • [\\s\\S] is used to match any character, including line limiter. It does better the simple dot . See the StackOverflow answer here.
  • [\\ud83c\\ud000-\\ud83c\\udfff]: We have to add prefix to both range start because I found the java syntax treats the regex as a single code point.

Originally published at https://blog.junjizhi.com.

--

--

No responses yet