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

  • Do you mean tabs and spaces mixed in with text, or tabs and spaces just on their own?
    – Wes Nolte
    Feb 26, 2013 at 13:49
  • mixed e.g. sund ay
    – Dedo
    Feb 26, 2013 at 14:09

3 Answers 3


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.

  • This is what I'm trying but it does not work REGEX(myField__c, "[\\s]+")
    – Dedo
    Feb 26, 2013 at 14:35
  • Odd. I'll see what I can come up with.
    – Mike Chale
    Feb 26, 2013 at 15:21
  • 4
    @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 Feb 27, 2013 at 1:16
  • @PeterKnolle Strange, it is not working. Maybe a salesforce bug? Feb 27, 2013 at 7:31
  • @PeterKnolle This is it: REGEX(myField__c ,".*\\s+.*"). Thanks
    – Dedo
    Feb 27, 2013 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.


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

  • 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, 2013 at 14:11
  • I tried contains but it didn't work. Is there any RegEx I can use?
    – Dedo
    Feb 26, 2013 at 14:16

You must log in to answer this question.

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