JTextField: How to validate number input in JTextField

1.4k views Asked by At

How to validate a textfield to enter only 10 digits mobile number in Swing?

I have three text field.

  1. For name
  2. For Contact
  3. No. For Email

I want to click on submit button then check name, contact and email right or wrong. I also want to set limit of character

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class UpDateProfile extends JFrame implements ActionListener
{
    JLabel name_lbl,email_lbl,contact_lbl;
    JTextField name_text,email_text,contact_text;
    JButton submit_btn;
    public UpDateProfile()
    {
            super("Velidation demo");

            setSize(650,450);
            setLocation((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()/2-325,(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()/2-225);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLayout(new GridBagLayout());
            setResizable(false);

            GridBagConstraints gbc1 = new GridBagConstraints();
            gbc1.insets = new Insets(5,3,5,3);

        name_lbl = new JLabel("Name :");
        gbc1.gridx = 0;
        gbc1.gridy = 0;
        gbc1.ipadx = 0;
        gbc1.ipady = 0;
        gbc1.gridheight = 1;
        gbc1.gridwidth = 1;
        gbc1.fill = GridBagConstraints.HORIZONTAL;
        gbc1.anchor = GridBagConstraints.WEST;
        add(name_lbl,gbc1);

        name_text = new JTextField(30);
        gbc1.gridx = 1;
        gbc1.gridy = 0;
        gbc1.ipadx = 0;
        gbc1.ipady = 0;
        gbc1.gridheight = 1;
        gbc1.gridwidth = 3;
        gbc1.fill = GridBagConstraints.HORIZONTAL;
        gbc1.anchor = GridBagConstraints.WEST;
        add(name_text,gbc1);

        email_lbl = new JLabel("E-mail :");
        gbc1.gridx = 0;
        gbc1.gridy = 1;
        gbc1.ipadx = 0;
        gbc1.ipady = 0;
        gbc1.gridheight = 1;
        gbc1.gridwidth = 1;
        gbc1.fill = GridBagConstraints.HORIZONTAL;
        gbc1.anchor = GridBagConstraints.WEST;
        add(email_lbl,gbc1);

        alt_email_text = new JTextField(30);
        gbc1.gridx = 1;
        gbc1.gridy = 1;
        gbc1.ipadx = 0;
        gbc1.ipady = 0;
        gbc1.gridheight = 1;
        gbc1.gridwidth = 3;
        gbc1.fill = GridBagConstraints.HORIZONTAL;
        gbc1.anchor = GridBagConstraints.WEST;
        add(alt_email_text,gbc1);

        contact_lbl = new JLabel("Contact No. :");
        gbc1.gridx = 0;
        gbc1.gridy = 2;
        gbc1.ipadx = 0;
        gbc1.ipady = 0;
        gbc1.gridheight = 1;
        gbc1.gridwidth = 1;
        gbc1.fill = GridBagConstraints.HORIZONTAL;
        gbc1.anchor = GridBagConstraints.WEST;
        add(contact_lbl,gbc1);

        contact_text = new JTextField(10);
        gbc1.gridx = 1;
        gbc1.gridy = 2;
        gbc1.ipadx = 0;
        gbc1.ipady = 0;
        gbc1.gridheight = 1;
        gbc1.gridwidth = 1;
        gbc1.fill = GridBagConstraints.HORIZONTAL;
        gbc1.anchor = GridBagConstraints.WEST;
        add(contact_text,gbc1);

        submit_btn = new JButton("Submit");
        submit_btn.addActionListener(this);
        gbc1.gridx = 2;
        gbc1.gridy = 7;
        gbc1.ipadx = 10;
        gbc1.ipady = 0;
        gbc1.gridheight = 1;
        gbc1.gridwidth = 2;
        gbc1.anchor = GridBagConstraints.CENTER;
        add(submit_btn,gbc1);
    }
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==submit_btn)
        {

        }
    }
}
1

There are 1 answers

2
Eric Leibenguth On BEST ANSWER
public void actionPerformed(ActionEvent ae)
{
    if(ae.getSource()==submit_btn)
    {
        String text = contact_text.getText();
        if(text.matches("\\d{10}")){
             // The text entered is a 10-digit number
        }else{
             // The text is not valid
        }
    }
}