+ Start a Discussion
sai.sfsai.sf 

String contains ONLY Number

Hi,

 

 I need to check whether a field of type string contains only number in trigger.Is there any method?

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
Saikishore Reddy AengareddySaikishore Reddy Aengareddy

use isNumeric string method..

 

String s = '123';
system.debug('Numerinc@@@@->'+s.isNumeric()); //returns true

 

String s = '123asd';
system.debug('Numerinc@@@@->'+s.isNumeric()); //returns false

All Answers

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

use isNumeric string method..

 

String s = '123';
system.debug('Numerinc@@@@->'+s.isNumeric()); //returns true

 

String s = '123asd';
system.debug('Numerinc@@@@->'+s.isNumeric()); //returns false

This was selected as the best answer
AshlekhAshlekh

Hi,

 

 I wish this code will be useful for you.

You can find a string contain only number or not by Regex.

 

String x = '123';
Pattern numberFormat = Pattern.Compile('[0-9]*');
Matcher numberMatch = numberFormat.matcher(x);

System.debug('----'+numberMatch.Matches()); // It print true as string x contain only number if x contain a single char or any space then it will return false

 

.Thanks

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

sai.sfsai.sf

Thanks Sam