Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I prevent spaces and tabs in a string field using a validation rule?

share|improve this question
Do you mean tabs and spaces mixed in with text, or tabs and spaces just on their own? – Wes Nolte Feb 26 '13 at 13:49
mixed e.g. sund ay – Dedo Feb 26 '13 at 14:09
up vote 4 down vote accepted

You can do this with regular expressions. The character class \s represents whitespace. A simple regex to search for any whitespace would be [\s]+ which will match 1 or more whitespace characters.

For more in-depth information on regular expressions I recommend this tutorial that is my primary reference.


As PeterKnolle pointed out in the comments, the correct regular expression should be .*\\s+.*. The dots are needed to match any and all other characters before or after the whitespace.

share|improve this answer
This is what I'm trying but it does not work REGEX(myField__c, "[\\s]+") – Dedo Feb 26 '13 at 14:35
Odd. I'll see what I can come up with. – Mike Chale Feb 26 '13 at 15:21
@Dedo and @MikeChale - It uses Java regular expressions, so you should be able to do REGEX(myField__c ,".*\\s+.*") in a validation rule, I think – Peter Knolle Feb 27 '13 at 1:16
@PeterKnolle Strange, it is not working. Maybe a salesforce bug? – Sergey Utko Feb 27 '13 at 7:31
@PeterKnolle This is it: REGEX(myField__c ,".*\\s+.*"). Thanks – Dedo Feb 27 '13 at 8:32

I needed to do this in a formula field (which can't use REGEX) and it's not elegant, but it does work and it should also work for your scenario:

SUBSTITUTE(SUBSTITUTE(Field__c, ' ', ''), ' ', '')

To use it in a validation rule, you just need to compare the result of this with the original string, if they're different, then the string must contain whitespace.

SUBSTITUTE(SUBSTITUTE(Field__c, ' ', ''), ' ', '') != Field__c

Note that the second ' ' is actually a tab, it just appears as a space here for some reason.

share|improve this answer

If you want to prevent that this field remains empty - try this formula:

share|improve this answer
not exactly, I want to make sure there are no spaces in between the characters e.g. "Ononm atopea" should be prevented because it has a space – Dedo Feb 26 '13 at 14:11
I tried contains but it didn't work. Is there any RegEx I can use? – Dedo Feb 26 '13 at 14:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.