Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 760
  • Last Modified:

validating a string before/after decimal point

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
jaggernat
Asked:
jaggernat
  • 8
  • 8
1 Solution
 
agriesserCommented:
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
 
jaggernatAuthor Commented:
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
 
agriesserCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jaggernatAuthor Commented:
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
 
agriesserCommented:
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
 
agriesserCommented:
oh, another thing: is the decimal point mandatory? if it isn't, just tell me and I'll modify the regex again.
0
 
jaggernatAuthor Commented:
thx, no the decimal point is not mandatory

thx
0
 
agriesserCommented:
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
 
jaggernatAuthor Commented:
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
 
agriesserCommented:
999999 has 6 digits and you said it just needs to have 5 at max.
0
 
jaggernatAuthor Commented:
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
 
agriesserCommented:
Sorry, my fault (not my best day as it seems):
"^[0-9]{0,5}(\\.[0-9]{0,2})?$"

Open in new window

0
 
jaggernatAuthor Commented:
it works, awesome. thanks, you did a great job actually.

0
 
jaggernatAuthor Commented:
any help with next question greatly appreciated

thanks
J
0
 
agriesserCommented:
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

  • 8
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now