Solved

validating a string before/after decimal point

Posted on 2008-10-07
16
710 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now