function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shawn Li 9Shawn Li 9 

how to count occurrence of a specific character in validation rules?

Dear Sir/Madam,
Suppose I have an input string "abc,efg,hijk", I want to count how many commas are included in the string (in this example, I want to return 2 since there are 2 commas). I searched around but found nothing. There is a similar post with no solution: https://developer.salesforce.com/forums/?id=906F0000000D8qKIAS

Thank you for the help.
Ankit SehgalAnkit Sehgal
Shawn Li 9Shawn Li 9
Hi Ankit, what I am looking for is a "COUNT" function. I do not think the above link actually solves the problem. Could you give an example? Thank you very much.
Michael PaisnerMichael Paisner

To count the number of commas (or any specific character) in a validation rule for the text field myfield__c, you can do the following:

LEN(myfield__c)-LEN(SUBSTITUTE(myfield__c,",",""))

The SUBSTITUTE function replaces the comma characters in the field with a zero-length string.  The length of the string after substitution subtracted from the original length of the string gives you the number of commas that are in the original string.

You can compare this to whatever you need for the validation rule.  If there should be NO commas, then your validation rule would be:

LEN(myfield__c)-LEN(SUBSTITUTE(myfield__c,",","")) > 0