Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

Currency Input Validation

Thread view: 
Dean - 28 Jul 2007 00:50 GMT
I apologise if this has already been covered.

How would I validate currency input from a JTextField. For example, to
ensure that a currency value was input in a currency format ##.##
instead of ####.

I've tried using matching with regex's but that didnt work very well.
It was able to ensure that the dot (.) was entered but allow more that
2 numbers after the dot.

Below is a class I created to handle currency validation.

public class CurrencyInput extends Input
{

   /** Creates a new instance of CurrencyInput */
   public CurrencyInput(String input)
   {
       super(input);
   }

   /** Currency regex format */
   private String currencyFormat()
   {
       return "[\\d]+[.][\\d][\\d]";
   }

   private boolean isCurrency()
   {
       boolean isCurrency = false;

       if (match(currencyFormat()))
           isCurrency = true;

       return isCurrency;
   }

   /** Determines whether the currency is in the correct format and
if it is valid */
   public boolean isCurrencyValid()
   {
       boolean isCurrencyValid = false;

       if (isCurrency())
           if (Double.parseDouble(input) > 0)
               isCurrencyValid = true;

       return isCurrencyValid;
   }

}

I apprecaite any assistance that is provided.
Arne Vajhøj - 28 Jul 2007 00:56 GMT
> I apologise if this has already been covered.
>
[quoted text clipped - 5 lines]
> It was able to ensure that the dot (.) was entered but allow more that
> 2 numbers after the dot.

>     private String currencyFormat()
>     {
>         return "[\\d]+[.][\\d][\\d]";
>     }

Try:

"^[ ]*[\\d]+[.][\\d][\\d]$"

Arne
Dean - 28 Jul 2007 02:36 GMT
On Jul 28, 12:56 am, Arne Vajh?j <a...@vajhoej.dk> wrote:
> > I apologise if this has already been covered.
>
[quoted text clipped - 15 lines]
>
> Arne

I made the change you suggested but unfortunately it still doesn't
work.
Arne Vajhøj - 28 Jul 2007 03:19 GMT
>>> I apologise if this has already been covered.
>>> How would I validate currency input from a JTextField. For example, to
[quoted text clipped - 13 lines]
> I made the change you suggested but unfortunately it still doesn't
> work.

Can you give a short working example that shows the problem ?

Arne
Dean - 28 Jul 2007 04:13 GMT
On Jul 28, 3:19 am, Arne Vajh?j <a...@vajhoej.dk> wrote:
> > On Jul 28, 12:56 am, Arne Vajh?j <a...@vajhoej.dk> wrote:
> >>> I apologise if this has already been covered.
[quoted text clipped - 20 lines]
>
> - Show quoted text -

Sorry mate, I was being an arse when I ran a test. Your solution works
perfectly. Thanks for your help mate.
Nigel Wade - 30 Jul 2007 09:50 GMT
>> >>> I apologise if this has already been covered.
>> >>> How would I validate currency input from a JTextField. For example, to
[quoted text clipped - 22 lines]
> Sorry mate, I was being an arse when I ran a test. Your solution works
> perfectly. Thanks for your help mate.

You might also want to consider a JFormattedTextField with a MaskFormatter.

There is a page on how to use these together to validate input in the Swing
tutorial:
<http://java.sun.com/docs/books/tutorial/uiswing/components/formattedtextfield.html>

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.