+ Start a Discussion
SFineSFine 

Validation rule: Only accepted characters

Hello everyone,

 

At the moment, I'm trying to implement a validation rule that would check to see that certain characters are used/not used.

 

The description is basically: Available Charactersare: 0-9   a-z   A-Z.  ,  -  /  &  [blank space]

 

I figure I would need to use REGEX for this, but I'm not completely familiar with REGEX, so after doing some reading and research I came up with this:

 

REGEX ( Business_Owner_First_Name__c , "[a-zA-Z0-9 // /, /& - / ] {50}")

 

Now I know I probably have a number of things wrong with this logic and the testing seems to suggest that.

 

If any REGEX experts might be able to help, I'd be very appreciative.

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

You can use this formula if you want to stop in case any of the charater exists

 

OR(CONTAINS(FIELdAPIName, ' ') , REGEX(FIELdAPIName,  "^[a-zA-Z0-9-/]*$"))

 This will return true if any of the specified charater is used in the text 

 

If you want to allow in case of any of the character user this

NOT(OR(CONTAINS(FIELdAPIName, ' ') , REGEX(FIELdAPIName,  "^[a-zA-Z0-9-/]*$")))

 

All Answers

Shashikant SharmaShashikant Sharma

You can use this formula if you want to stop in case any of the charater exists

 

OR(CONTAINS(FIELdAPIName, ' ') , REGEX(FIELdAPIName,  "^[a-zA-Z0-9-/]*$"))

 This will return true if any of the specified charater is used in the text 

 

If you want to allow in case of any of the character user this

NOT(OR(CONTAINS(FIELdAPIName, ' ') , REGEX(FIELdAPIName,  "^[a-zA-Z0-9-/]*$")))

 

This was selected as the best answer
SFineSFine

Thanks. That did the trick.

Shashikant SharmaShashikant Sharma

Your welcome

SFineSFine

One last thing - that particular formula doesn't seem to work for blank spaces like ' '. Is there a way to tell it to include those?

Shashikant SharmaShashikant Sharma

It is working for me can you provide what formula you have used. Out of two that I provided.

SFineSFine

The code I'm using is:

 

not(REGEX( Business_Owner_First_Name__c , "^[a-zA-Z0-9-/]*$"))

Shashikant SharmaShashikant Sharma

Why don't you use

 

NOT(OR(CONTAINS(FIELdAPIName, ' ') , REGEX(FIELdAPIName,  "^[a-zA-Z0-9-/]*$")))

 Yes this does not check space with REGEX but will workfine for your case as we have separately checked that. 

SFineSFine

That helps, though i had to do some tweaking to make it considering both ' ' and invalid characters. If invalid characters are there, but with an blank space, then it'll pass the validation rule. Changing the or to and fixes this.

 

However, now I need  to check for multiple fields with the same logic. Below is the validation code I have for it. I feel like I'm just one step away from making this work.

 

not(
   and(
      and(CONTAINS(Business_Owner_First_Name__c, ' '), REGEX( Business_Owner_First_Name__c , "^[a-zA-Z0-9-/]*$")),
      and(CONTAINS(Business_Owner_Last_Name__c, ' '), REGEX( Business_Owner_Last_Name__c , "^[a-zA-Z0-9-/]*$")),
      and(CONTAINS( Applicant_s_First_Name__c, ' '), REGEX( Applicant_s_First_Name__c , "^[a-zA-Z0-9-/]*$")),
      and(CONTAINS( Applicant_s_Last_Name__c, ' '), REGEX( Applicant_s_Last_Name__c , "^[a-zA-Z0-9-/]*$")),
      and(CONTAINS(Card_Report_First_Name__c, ' '), REGEX( Card_Report_First_Name__c , "^[a-zA-Z0-9-/]*$")),
      and(CONTAINS(Card_Report_Last_Name_Suffix__c, ' '), REGEX( Card_Report_Last_Name_Suffix__c , "^[a-zA-Z0-9-/]*$"))))

Ankit AroraAnkit Arora

Not sure it will work for you or not, but its working for me :

 

NOT(REGEX( Business_Owner_First_Name__c , "^[a-zA-Z0-9-/ ]*$"))

 Just added a space in regex.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page