+ Start a Discussion
Stephanie Zimmerman 9Stephanie Zimmerman 9 

Regex Formula Validation to Require [A-Z][a-z] - and space

Hi!

I would love to get some quick help with this validation rule:

NOT( 
OR( 
ISBLANK(Last_Name__c),REGEX(Last_Name__c ,"([a-zA-Z-])*")))

What I want is for Last Name to include only a-z, A-Z, hyphens or spaces. 

When I first wrote it as this: REGEX(Last_Name__c ,"([a-zA-Z])*")))  -- without the hyphen, I was getting it to work to allow spaces. However, once I added the hyphen in, the hyphen was allowed but spaces were no longer allowed. 

What am I doing wrong here? 

Thanks!
Stephanie
Best Answer chosen by Stephanie Zimmerman 9
CloudGeekCloudGeek
Hello Stephanie,

Try this one:
NOT( 
OR( 
ISBLANK(Name),REGEX(Name ,"([a-zA-Z- ])*")))

Notice the SPACE after HIPHEN in REGEX [a-zA-Z- ].

Hope that helps!

All Answers

CloudGeekCloudGeek
Hello Stephanie,

Try this one:
NOT( 
OR( 
ISBLANK(Name),REGEX(Name ,"([a-zA-Z- ])*")))

Notice the SPACE after HIPHEN in REGEX [a-zA-Z- ].

Hope that helps!
This was selected as the best answer
BarnesMichaelLBarnesMichaelL
This was super helpful.  Thanks!  I adjusted mine to: "([a-zA-Z0-9- ])*" to also allow numeric values, but no other special characters.
Curtis Constant 3Curtis Constant 3
My formula below works but, how do I allow for just the first name to be blank?

AND( 
Account.IsPersonAccount = FALSE, 
Account.Name != "BCBSMA", 
OR( 
not(REGEX( LastName, "[a-zA-Z\\.\\ \\'\\-\\,]+")), 
not(REGEX( FirstName, "[a-zA-Z\\.\\ \\'\\-\\,]+")) 

)
Reshmi Sangani 5Reshmi Sangani 5
Hello I am trying to write a validation rule using regex
how do I write a validation rule to prevent users from entering "Test" in the Opportunity Name field
The rule should fire when, 
field = 'TEST'
field = 'test'
field = 'micro test opportunity'
field = 'micro-test'
should not fire when 
field = 'protest'
field = testimony'

Thank you!