cancel
Showing results for 
Search instead for 
Did you mean: 

Simple Numeric validation logic required !

Former Member
0 Kudos

Hi All ,

Need quick and simple logic for numeric validation.

I have to check the field areacode contains values between 0-9 only and it receives only 3digits.Please tell me how to write this logic.

String Area = wdContext.currentSelectedInfotypeElement().getAreac();

if (fpm.getEventData().getEventName().equals(IFPM.EVENT_BACK))

{

Area.matches("0-9");

if(Area.equals("345"))

{

return true;

}

}

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

java.util.regex.Pattern NUM3 = Pattern.compile("\\d{3}");
java.util.regex.Matcher m = NUM3.matcher(<some string>);
m.matches(); // returns true if <some string> matches the NUM3 pattern

Armin

Former Member
0 Kudos

Hi Armin ,

Even it reaches true for combination of alphabet and numbers. Please help me.

Former Member
0 Kudos

I think you are doing something wrong. Try this little test app:


package armin;

public class Pattern1
{

  /**
   * @param args
   */
  public static void main(String[] args)
  {
    check("");
    check("123");
    check("abc");
    check("1");
    check("1ab");
  }
  
  private static void check(String s)
  {
    java.util.regex.Matcher m = NUM3.matcher(s);
    System.out.println(s + (m.matches() ? " matches " : " does not match ") + NUM3); 
  }

  private static java.util.regex.Pattern NUM3 = java.util.regex.Pattern.compile("\\d{3}");

}

Armin

Former Member
0 Kudos

A numeric value with 3 digits, using only 0-9 is an integer >0 and <1000, or do I get the question wrong? Why the fuzz?

If you need more control, define a simple type based on integer and set upper and lower bounds. If you also set the NumberFormat, you will even get the leading zeros right ...

Former Member
0 Kudos

hi

check the field areacode contains values between 0-9 only and it receives only 3digits ,

you can use pattern matching code to solve your issue , there are many sample application you can find in google

sdn or even check the java api ,

or other thing you can do is use ASCII values to get only numerix values , if condition between 0 to 9 where

0 to 9 matches ASCII values ,

Former Member
0 Kudos

Hi ,

Thanks for reply. I dnt know much about java. If you can provide code lines , it will be helpful.

Former Member
0 Kudos

Hi Shruthi,

Try this code hope it will help and do the necessary correction if required.


String expression = "([0-9]+)";
	CharSequence inputStr = "345" // supply your input String Here.;
			Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
			Matcher matcher = pattern.matcher(inputStr);

	return matcher.matches();

Let us know if you need any further assist.

Regards

Jeetendra

Former Member
0 Kudos

you may need to import

import java.util.regex.Matcher;

import java.util.regex.Pattern;

please import it.

Former Member
0 Kudos

Thanks you for the code. But it return true even if we give Alphabets in it. Please help me in resolving.

Edited by: Shrushti on Feb 3, 2010 5:34 PM

Former Member
0 Kudos

String expression = "([0-9]+)";

CharSequence inputStr = "345" // supply your input String Here.;

Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);

Matcher matcher = pattern.matcher(inputStr);

return matcher.matches();

Requirement is : If i give y7y (alphabet and number) it should return false.