How To Create a Validation Rule to Enter Whole Numbers in a Field

regular-expressionsvalidation

I have a custom field called GMPI__c and I need to create a validation rule with the following requirements.

  1. User can enter numbers 0-9 only.
  2. The field is NOT required, so it can be left blank
  3. No special characters, decimals, commas, or negative numbers.
  4. Number must also be unique
  5. Data type can be either number or text

I created the field with a text data type and number data type and I'm running into problems with both. Can someone help me figure this out?

Here is the Text data type. It meets all requirements except the decimals are still present in the number.

NOT(
   OR(
      ISBLANK(GMPI__c),
      VALUE(GMPI__c) > 0,
      REGEX(TEXT(VALUE(GMPI__c)), "^[+]?\\d+$")
      )
)

Here is the Number data type. It meets all requirements except the commas are still showing up in the number.

NOT(
    OR( 
        REGEX(TEXT(GMPI__c), "^[+]?\\d+$"),  
        GMPI__c > 0,
        ISBLANK(GMPI__c)
        )
)

Best Answer

Please let me know is this stack over flow question answer your question?

https://stackoverflow.com/questions/57991064/salesforce-validation-rule-regex

Thanks.