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
Diptonil Deka 8Diptonil Deka 8 

how to restrict specific string characters on name fields?

my requirement is: 
As a user, i want to be able to enter proper name in First name, Middle name or Last name boxs in Personal details section on an in Salesforce Given that the details can be created manually or can be uploaded from the Backend.
When i try to edit First, middle or Last name on Personal details section in the object  then if i  enter dr/DR/dR/MR/mr/Mr or Test or Clinic or Dummy or any special character or space or numeric input as a Prefix and click on save then it should throw error as: 

 "can't add Dr/Mr or  Test or Clinic or Dummy or any special characer or space or numeric input as prefix or suffix "
Best Answer chosen by Diptonil Deka 8
CharuDuttCharuDutt
Hii Diptonil Deka
Try The Below Validation Rule
OR(CONTAINS( Name , 'DR'),CONTAINS( Name , 'MR'),CONTAINS( Name , 'dr'),CONTAINS( Name , 'mr'),CONTAINS( Name , 'Test'),CONTAINS( Name , 'Clinic'),CONTAINS( Name , 'Dummy'), AND(NOT(REGEX( Name , "^[a-zA-Z_]*$"))))
Please Mark It As Best Answer If It Helps
Thank You! 

 

All Answers

CharuDuttCharuDutt
Hii Diptonil Deka
Try The Below Validation Rule
OR(CONTAINS( Name , 'DR'),CONTAINS( Name , 'MR'),CONTAINS( Name , 'dr'),CONTAINS( Name , 'mr'),CONTAINS( Name , 'Test'),CONTAINS( Name , 'Clinic'),CONTAINS( Name , 'Dummy'), AND(NOT(REGEX( Name , "^[a-zA-Z_]*$"))))
Please Mark It As Best Answer If It Helps
Thank You! 

 
This was selected as the best answer
ANUTEJANUTEJ (Salesforce Developers) 
Hi Diptonil,

I think you can either use a validation rule or trigger for trigger you can try the below snippet:
 
trigger objTri on object__c(before update)
{
if(trigger.isbefore && trigger.isupdate)
{
for(Object__C temp: trigger.new)
{
//dr/DR/dR/MR/mr/Mr or Test or Clinic or Dummy or any special character or space or numeric input

if(temp.firstname.containsIgnoreCase('dr')|| temp.firstname.containsIgnoreCase('mr') || temp.firstname.containsIgnoreCase('Test') || temp.firstname.containsIgnoreCase('clinic') || temp.firstname.containsIgnoreCase('dummy') || temp.firstname.containsAny('1234567890!@#$%^&*()'))
{temp.adderror('can not add Dr/Mr or  Test or Clinic or Dummy or any special characer or space or numeric input as prefix or suffix');}
}
}
}

Please do note that I have added only for the first name you will have to add the conditions for middle and last name as well as per the scenario you are implementing.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Suraj Tripathi 47Suraj Tripathi 47
Hi Diptonil,

You can provide the valdition on input field and check the value onkeypress  or onchange event if the value matches with your validation rule than that must be enter otherwise they provide some error.

for your reference i provide you some input field validation program links:
http://salesforcecodes.com/input-text-restrictnumber-alphabets-special-characters-keyboard-typing-in-lightning-component/
https://www.infallibletechie.com/2020/10/how-to-restrict-users-to-avoid-entering.html 

If you find your answer than mark as a best answer
Thanks And Regards,
Suraj Tripathi.
Diptonil Deka 8Diptonil Deka 8
for me the validation rule worked golden: 

AND(OR(CONTAINS( First_Name__c , 'DR'),CONTAINS( First_Name__c , 'MR'),CONTAINS( First_Name__c , 'Dr'),CONTAINS( First_Name__c , 'Mr'),
CONTAINS( First_Name__c , 'Test'),CONTAINS( First_Name__c , 'Clinic'),CONTAINS( First_Name__c , 'Dummy'),NOT(REGEX( First_Name__c , "^[a-zA-Z_]*$")),CONTAINS( Middle_Name__c , 'DR'),CONTAINS( Middle_Name__c , 'MR'),CONTAINS( Middle_Name__c , 'Dr'),CONTAINS( Middle_Name__c , 'Mr'),CONTAINS( Middle_Name__c , 'Test'),
CONTAINS( Middle_Name__c , 'Clinic'),CONTAINS( Middle_Name__c , 'Dummy'),NOT(REGEX( Middle_Name__c , "^[a-zA-Z_]*$")),CONTAINS( Last_Name__c , 'DR'),CONTAINS( Last_Name__c , 'MR'),
CONTAINS( Last_Name__c , 'Dr'),CONTAINS( Last_Name__c , 'Mr'),CONTAINS( Last_Name__c , 'Test'),CONTAINS( Last_Name__c , 'Clinic'),
CONTAINS( Last_Name__c , 'Dummy'),NOT(REGEX( Last_Name__c , "^[a-zA-Z_]*$"))),(RecordType.Name = 'Doctor'))