简体   繁体   中英

How to restrict user to enter digits or special charater or blank or space in JTextField in Java Swing?

I am working on a project in Java Swing. Now there is a text field for user name. I want to validate that text field so that user can't enter digits or special character or blank or space . In short I want a restriction such that user can't enter anything other than alphabets. The database is MS Access and table name is "test" and there is only one column named "sname". As I am a beginner, simpler techniques will be appreciated.Thanks in advance.

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

public class Ex_test extends JFrame implements ActionListener
{
    public static void main(String[] args) 
    {
        Ex_test ob=new Ex_test();
    }
    JTextField tf1;
    JButton b1;
    int num1;
    public Ex_test()
    {
    super("test");
    setLayout(new FlowLayout());

    tf1=new JTextField(20);
    add(tf1);

    b1=new JButton("ok");
    add(b1);

    setSize(500,500);
    setVisible(true);

    b1.addActionListener(this);

    }
    public void actionPerformed(ActionEvent ae)
    {
        if (ae.getActionCommand()=="ok")
        {   
            try
            {   
                String str=tf1.getText();
                Connection con;
                PreparedStatement ps;
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                con=DriverManager.getConnection("jdbc:odbc:test");
                ps=con.prepareStatement("insert into test values('"+str+"')");
                ps.executeUpdate();
                con.close();
                ps.close();
                JOptionPane.showMessageDialog(null,"DATA SUBMITTED SUCCESSFULLY.");
            }
            catch (Exception e)
            {
                JOptionPane.showMessageDialog(null,"Exception Occurred.");
            }

        }
    }
}

Use a JFormattedTextField. Read the section from the Swing tutorial on How to Use Formatted Text Fields for more information and examples.

You can use a MaskFormatter to specify that you only want to allow alphabetic characters.

Try something like this - i used regex which validates input from user, more about regex you will find here : http://www.vogella.com/tutorials/JavaRegularExpressions/article.html

try
        {   
            String str=tf1.getText();
            if ( !(str.matches("[a-zA-Z]+"))) {
                  JOptionPane.showMessageDialog(null,"Please insert only characters.");
                  tf1.setText("");
            }else{

            Connection con;
            PreparedStatement ps;
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con=DriverManager.getConnection("jdbc:odbc:test");
            ps=con.prepareStatement("insert into test values('"+str+"')");
            ps.executeUpdate();
            con.close();
            ps.close();
            JOptionPane.showMessageDialog(null,"DATA SUBMITTED SUCCESSFULLY.");}
        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(null,"Exception Occurred.");
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM