DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Kamal Vaid Guest

    Validation in textfield(numeric)


    Hello Everybody

    I had problem ,how to validate the double or float in textfield
    like (-123.45 or 45.56).Please tell me it urgent and implement
    in project as I new java programmer but exception handling for numeric

    thanks

  2. #2
    a albert77 Guest

    Re: Validation in textfield(numeric)


    set as double

    hope it helps

    "Kamal Vaid" <vaidkamal@hotmail.com> wrote:
    >
    >Hello Everybody
    >
    >I had problem ,how to validate the double or float in textfield
    >like (-123.45 or 45.56).Please tell me it urgent and implement
    >in project as I new java programmer but exception handling for numeric
    >
    >thanks



  3. #3
    Bryan Baas Guest

    Re: Validation in textfield(numeric)


    "Kamal Vaid" <vaidkamal@hotmail.com> wrote:
    >
    >Hello Everybody
    >
    >I had problem ,how to validate the double or float in textfield
    >like (-123.45 or 45.56).Please tell me it urgent and implement
    >in project as I new java programmer but exception handling for numeric
    >
    >thanks


    Here's a little code for you. What you want to do is use the Float "object
    wrapper" parseFloat method and catch a NumberFormatException. Enjoy.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class Test extends JFrame implements ActionListener
    {
    public Button enter = new Button("Enter");
    public TextField textObj = new TextField(25);

    Test()
    {
    getContentPane().setLayout(new FlowLayout());
    enter.addActionListener(this);
    getContentPane().add(textObj);
    getContentPane().add(enter);
    pack();
    };

    public void actionPerformed(ActionEvent evt)
    {
    String text = textObj.getText();

    // validate the number here
    try
    {
    float val = Float.parseFloat(text);
    textObj.setText("Valid float entered");
    }

    // handle exception here
    catch (NumberFormatException e)
    {
    textObj.setText("NumberFormatException"); }
    };

    public static void main(String[] args)
    {
    Test testObj = new Test();
    testObj.show();
    };

    };


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links