Solved

validating a string before/after decimal point

Posted on 2008-10-07
16
742 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
Technology Partners: 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!

 
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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Maven Project: Hibernate Dependencies Conflict 10 49
hibernate example issues from command prompt 10 63
iterator/ListIterator approach 17 50
running on tomcat not jboss eap 7.0 3 28
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

679 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