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
Chad CoffeyChad Coffey 

Validation to only allow whole numbers

I am working on a validation on a number field that is apart of a managed package so we cannot change the number of decimals.  I need to the field to always have a whole number entered.  The validation I have below, works for the most part, except when 10,20,30 are entered. The Validation work corrects when 15,25, etc are entered.  Any help? 

AND( 
NOT(REGEX(TEXT( jstcl__Burden__c ), "[0-9]+[.]{1}[0-9]{2}")), 
NOT( jstcl__Burden__c =0))
Karthik KKarthik K
Can you try this

AND( 
NOT(REGEX(TEXT( jstcl__Burden__c ), "^[+]?\d+$")), 
NOT( jstcl__Burden__c =0))
Chad CoffeyChad Coffey
Thank you for the response.  That is kicking me a syntax error. 

User-added image
Alain CabonAlain Cabon
Karthik K just forgot to double the antislash.

AND( 
NOT(REGEX(TEXT( jstcl__Burden__c ), "^\\d+$")),   // only a whole number without fractional part
NOT( jstcl__Burden__c = 0))

[+]?  = Greedy quantifiers = X? : once or not at all, for an optional positive sign but that is not useful here. 

https://docs.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html

Another solution could be like that if a fractional part could exist and the decimal part must be greater than zero.

NOT( jstcl__Burden__c - mod( jstcl__Burden__c , 1 ) > 0 )   // a number minus its fractional part > 0

mod( jstcl__Burden__c , 1 ) is the fractional part of jstcl__Burden__c.
Chad CoffeyChad Coffey
Alain, thanks for your help. 

The first suggestion does not allow any value in the jstcl__Burden__c field to be entered and saved.  The second suggestion I could not get to work properly either because it allows any value to be entered aka 20.25%.  So the decimal in the percentage should always be entered with 0's
Alain CabonAlain Cabon
Chad,

It is difficult to find the correct answer because your requirements are not clear.

What is your field type?

What values are permitted?  (only whole numbers?)

Of course,  20.25,  is permitted if you use NOT( jstcl__Burden__c - mod( jstcl__Burden__c , 1 ) > 0 ) (means "a fractional part could exist and the decimal part must be greater than zero." as written above)

if 20.00 is just allowed, the fractional part must be equals to zero and the decimal part greater than zero, 

OR ( NOT( jstcl__Burden__c - mod( jstcl__Burden__c , 1 ) > 0 )  , mod( jstcl__Burden__c , 1 ) >  0 )

The Karthik K's formula is correct as soon as you double the antislash but what is your field type and your locale language (French people use comma instead of period (point) for number, it is the exact opposite of the Americans) ?

I tested the two solutions with numbers.

A percent value is in fact a value between 0 and 1 mutliply by 100.  That is your main problem because the formula above are for number (greater than 1)
Chad CoffeyChad Coffey
Sorry for any confusion.  The field  jstcl__Burden__c is a percentage field with 2 decimals.  This is a managed field by one of our vendors so I cannot simply change the Decimal Places to 0.  We need to disallow our users from entering a value such as 25.25%.  They should only enter a value such as 25 or 20.  The system will automatically turn the 25 & 20 into 25.00 and 20.00.  Essentially we need the two decimal places (hundredths/thousands) to = .00. 

 
Harsha RevannaHarsha Revanna

Alain Cabon
AND( 
NOT(REGEX(TEXT( Quantity ), "^\\d+$")), 
NOT( Quantity =0))

This Validation Rule Worked For My problem Much Appreciated 
In Opportunity Product Object There is a field called Quantity , A client requirement was "They Did not want to see Decimal Value like 25.65" so the above validation rule helped me to restric user to enter 25.65 however it will become a Whole Number i.e : 25.00 when record is saved.