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
drk9999drk9999 

Problem with Custom text field created for numbers only

Hi All,

 

I have created custom text field (18 characters) to accept only numbers, as we want to remove comma saperator in numbers. It is done sucessfully using the REGEX using following validation. It is not accepting any thing except numbers (which is good), but it is not saving if I enter less that 18 numbers. I have to enter 6 or 9 numbers according to requirement. please help, by suggesting better validation code or some other idea.

 

NOT(

 OR(

  LEN ( Only_Numbers__c ) = 0,

  REGEX( Only_Numbers__c , "[0-9]{18}")

 )

)

Best Answer chosen by Admin (Salesforce Developers) 
netTrekker_ADnetTrekker_AD

 

NOT(ISBLANK(Email_Control_3__c) ||(ISNUMBER (Email_Control_3__c )
&& LEN (Email_Control_3__c ) <= 18))

 

I tested this on a custom text field in my environment and it worked.

 

All Answers

netTrekker_ADnetTrekker_AD

The issue with REGEX() is that the number in {  } is the required number of characters, not the maximum number of characters.

 

I am not entirely familiar with where REGEX can be used but will it accept something such as {<=18} ?

netTrekker_ADnetTrekker_AD

How about making it a text field then creating a validation rule such as this:

 

 

ISBLANK(Only_Numbers__c)
|| NOT( ISNUMBER (Only_Numbers__c) && LEN (Only_Numbers__c) <= 18)

 

No matter what the user types, this rule will not allow the user to save the changes to the field Only_Numbers__c if the field is blank OR if it does not contain a string of only numbers OR over 18 characters long.

 

 

Let me know if this works and mark it solved if so. Thanks.

 

 

drk9999drk9999

Thank you for providing me a solution. I have used one more solution, but the problem with both of these are it is not allowing  me to save the page when this particular field is empty. Is there any way validation rule can be modified to fix this.

netTrekker_ADnetTrekker_AD

So you want it to be OK for the object to be saved with the field blank?

 

 

NOT(
ISNUMBER (Only_Numbers__c)
&& LEN (Only_Numbers__c) <= 18)

 

The first line of the previous code was reading "if the field is blank, throw the error". I thought that is what you wanted due to your original code containing the LEN() = 0 function which is basically the same thing.

 

drk9999drk9999

I am not good at making the code, it was what i have seen in help before. But later I have figured what i needed.

 

I tried the new rule, it is not working with empty field either. please help.

 

Thanks

netTrekker_ADnetTrekker_AD

 

 

NOT (ISBLANK(Only_Numbers__c ))
|| NOT(ISNUMBER (Only_Numbers__c)
   && LEN (Only_Numbers__c) <= 18)

 

Let me know if this works. Its hard for me to test it because I do not have the same field.

drk9999drk9999

It is not even allowing me to save with numbers and blank both.

 

It is just a Text field type, where i want to just enter numbers or leave it blank to save.

netTrekker_ADnetTrekker_AD

 

NOT(ISBLANK(Email_Control_3__c) ||(ISNUMBER (Email_Control_3__c )
&& LEN (Email_Control_3__c ) <= 18))

 

I tested this on a custom text field in my environment and it worked.

 

This was selected as the best answer
drk9999drk9999

Perfect, it works good. Thanks you once again. I will let you know if this give any trouble in future when deployed.

 

Thanks Again.