cancel
Showing results for 
Search instead for 
Did you mean: 

How to check input for Alphanumeric

Former Member
0 Kudos

Hello All,

Can some one tell me how to check whether the Input coming is String or Alphanumeric

Regards,

Moorthy

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Moorthy,

There is no standard mapping function to check the alphanumeric. You need to write a small UDF to do this function.

here is small code: this UDF takes one input(inString) value for which we check whether it is numeric or alphanueric


	Boolean result = false;
		 try  
		   {   
		      Long.parseLong(inString);   
		      result = true;
		      return result.toString();   
		   }   
		   catch(Exception e)   
		   {  
			  result = false;
		      return result.toString();   
		   } 

Above UDF gives true is inString is Numeric or false if it's not numeric (it means, inString is Alphanumeric)

Thanks,

RK

baskar_gopalakrishnan2
Active Contributor
0 Kudos

Other alternate options ..

1 : use a regular expression (a more elegant solution).

public static boolean isNumeric(String inputData) {
  return inputData.matches("[-+]?\\d+(\\.\\d+)?");
}

2:use the positional parser in the java.text.NumberFormat object (a more robust solution). If, after parsing, the parse position is at the end of the string, we can deduce that the entire string was a valid number

public static boolean isNumeric(String inputData) {  
NumberFormat formatter = NumberFormat.getInstance(); 
 ParsePosition pos = new ParsePosition(0); 
 formatter.parse(inputData, pos); 
 return inputData.length() == pos.getIndex();
}

3: use the java.util.Scanner object. Very useful if you have to scan multiple entries.

public static boolean isNumeric(String inputData) {  
Scanner sc = new Scanner(inputData);  
return sc.hasNextInt();
}

Scanner also has similar methods for longs, shorts, bytes, doubles, floats, BigIntegers, and BigDecimals as well as methods for integral types where you may input a base/radix other than 10 (10 is the default, which can be changed using the useRadix method).

Answers (1)

Answers (1)

Former Member

one more option...

input : var1

execution type: single value



for (int i = 0; i < var1.length(); i++) 
{
if(Character.isDigit(var1.charAt(i)))
return "input is alphanumeric";
}
return "Input is string";

former_member641556
Discoverer
0 Kudos

A bit of correction.Thanks Amit your code worked perfectly.

for(int i=0;i< var1.length();i++){if(Character.isDigit(var1.charAt(i)))return"input is numeric";}return"Input is alphanumeric";