+ Start a Discussion

How to check whether field value is numeric or Alphnumeric in apex

how to find field value whether it is numeric or Alphnumeric in apex.
I have requirements like if FIELD1 = 123, FIELD2=456, display FIELD1 value in OUTPUTFIELD=123, if FIELD1=A123 or 123A, FIELD2=456 then display FIELD2 value in OUTPUTFIELD=456. 
Please guide me.
Shruti SShruti S
You can use isNumeric() method provided by String class in Apex. It returns a true if the string is a number and returns a false if the string contains an alphabet in it. Here is the code for your above use case -
if( field1__c.isNumeric() && field2__c.isNumeric() ) {
    outputfield__c = field1__c;
else if( !field1__c.isNumeric() && field2__c.isNumeric() ) {
    outputfield__c = field2__c;