cancel
Showing results for 
Search instead for 
Did you mean: 

UDF for finding whether a field value is contains only numeric or alphanum?

Former Member
0 Kudos

Hello,

Please help in creating the simple UDF for a Alphanumeric field contains only numeric characters or mix of numeric and alphanumerics?

Regards,

Jilan

Accepted Solutions (1)

Accepted Solutions (1)

SudhirT
Active Contributor
0 Kudos

Try like this

String chkValue = input.subString(0,1);
String flag = "false";
if ((chkValue >= 'A' && chkValue <= 'Z') || (chkValue >= 'a' && chkValue <= 'z')) {
flag = "true";
                      return flag;
                    }
else
{return flag;}

This will return true if the first character of input is alphabets and false if it is numeric value use this logic according to your requirement.

Thanks!

Answers (4)

Answers (4)

Former Member

Hi,

you could use this one:

> int i = 0;

> try {

> i = Integer.parseInt (value);

> } catch (Exception E){

> return "0";

> }

> return "1";

Regards

Patrick

Former Member
0 Kudos

Friends, Thanks for the help! Its sorted out!

Former Member
0 Kudos

Hi!

Which of the suggested solutions did you use? Or did you find another way?

I've the challenge to check a source field for only digits and blanks ...

Thanx in advance!

Regards,

Volker

Former Member
0 Kudos

Hi,

my code checks for numeric values.

If you have spaces only at the beginning or the end you could use the trim function first.

If you could also have values like for example 452 45642 55 then you could use the replace function and replace " " with "".

Using the result of these functions as input for the UDF should return your value.

Regards

Patrick

Former Member
0 Kudos

HI ,

You can try with this also.

Input to the UDF is String s and then write the following code...



if(s.matches("\\p{Alnum}+"))
 return 1;
else
 return 0;

if the value is one then that is Alphanumeric.

Regards

Goli Sridhar

Former Member
0 Kudos

HI Jilan,

Try with the following code.

String s is a input to the UDF then write the code to following

 
char[] chars = s.toCharArray();
  for (int i = 0; i < chars.length; i++) {      
    final char c = chars<i>;
    if ((c >= 'a') && (c <= 'z')) continue; // lowercase
    if ((c >= 'A') && (c <= 'Z')) continue; // uppercase
    if ((c >= '0') && (c <= '9')) continue; // numeric
    return false;
  }  
  return true;
} 

If it is Alphanumeric then it will return true otherwise it return false.

Regards

Goli sridhar