Solved

validating a string before/after decimal point

Posted on 2008-10-07
16
735 Views
Last Modified: 2008-10-07
hi guys

In my java class, I  have a field called
String amount = null ;


I have to validate the amount and make sure that amount folows the format xxxxx.xx
meaning it can have maximum 5 digits before the decimal and maximum of 2 digits after decimal
There fore the maximum a user can enter for amount is 99999.99

Any idea how i can do that.

Thanks very much
J
0
Comment
Question by:jaggernat
  • 8
  • 8
16 Comments
 
LVL 14

Expert Comment

by:agriesser
ID: 22659887
You could use regular expressions for that (supported since JDK 1.4):

The test amount "12345.99" will match, anything that's not 5.2 will not match.

hth
String amount = "12345.99";
if(amount.matches("^[0-9]{5}\\.[0-9]{2}$"))
  System.out.println("Matches.");
else
  System.out.println("Nope.");

Open in new window

0
 
LVL 10

Author Comment

by:jaggernat
ID: 22660292
ok, thanks. and also if i want to validate that the entered string is a number (zero to 99999.99) and not some garbage, , is there any reg ex to validate that too?

thanks v much
0
 
LVL 14

Expert Comment

by:agriesser
ID: 22660489
the above regex does exactly that.

asdfg.12  not valid
12345.1a not valid
123456. not valid
12300.  not valid
00001.00  valid
99999.99  valid
999.0  not valid
$1234.56  not valid
-1134.66 not valid

you get the idea :)
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 10

Author Comment

by:jaggernat
ID: 22660842
ok, thanks , got it. all of the above conditions are fine except

12300.  not valid  --> this should be valid coz before decimal it has 5 digits and after decimal it has none which is fine. The validation rule is  such that only if more than 5 digits are entered before the decimal and more than 2 digits are entered after the decimal , it should throw error.

so similarly 999.0  is valid coz  999 (less than 3 digits is valid)  and 0 (one digit after decimal is valid)

thanks

0
 
LVL 14

Expert Comment

by:agriesser
ID: 22660953
Oh, ok, so the regex needs to be changed to:
untested, am currently not in front of my pc.
"^[0-9]{,5}\\.[0-9]{,2}$"

Open in new window

0
 
LVL 14

Expert Comment

by:agriesser
ID: 22660971
oh, another thing: is the decimal point mandatory? if it isn't, just tell me and I'll modify the regex again.
0
 
LVL 10

Author Comment

by:jaggernat
ID: 22661203
thx, no the decimal point is not mandatory

thx
0
 
LVL 14

Expert Comment

by:agriesser
ID: 22662255
OK, back at my computer, that's the solution:
String amount = "12345";
if(amount.matches("^[0-9]{0,5}\\.?[0-9]{0,2}$"))
  System.out.println("Matches.");
else
  System.out.println("Nope.");

Open in new window

0
 
LVL 10

Author Comment

by:jaggernat
ID: 22662567
thx, i entered 999999
and  it did not go inside the IF condition
if(amount.matches("^[0-9]{0,5}\\.?[0-9]{0,2}$"))
{

}
not sure whats wrong
0
 
LVL 14

Expert Comment

by:agriesser
ID: 22662593
999999 has 6 digits and you said it just needs to have 5 at max.
0
 
LVL 10

Author Comment

by:jaggernat
ID: 22662688
exactly, so it should go inside my if statement. ok, this is my exact IF  statement

String amount= formBean.getColumn(fld);  // amount value is 999999            
 if (!amount.trim().equals("")  &&   !amount.matches("^[0-9]{0,5}\\.?[0-9]{0,2}$"))
{
//show error
if user enters 999999 , it should come here coz its 6 digits (max entered can be 5)  
}

thx
0
 
LVL 14

Accepted Solution

by:
agriesser earned 500 total points
ID: 22662776
Sorry, my fault (not my best day as it seems):
"^[0-9]{0,5}(\\.[0-9]{0,2})?$"

Open in new window

0
 
LVL 10

Author Comment

by:jaggernat
ID: 22662962
it works, awesome. thanks, you did a great job actually.

0
 
LVL 10

Author Comment

by:jaggernat
ID: 22663009
any help with next question greatly appreciated

thanks
J
0
 
LVL 10

Author Comment

by:jaggernat
ID: 22663012
0
 
LVL 14

Expert Comment

by:agriesser
ID: 22663030
Sorry that it took me a few tries to provide the correct solution, I'm stuffed up with work currently... but good to hear that it finally works for you!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question