简体   繁体   中英

Cannot validate JTextField's value using Regular expression while using Java Swing components

I am creating a simple Java Swing application to validate an IP Address. I added a JTextField , JButton , JLabel . JTextField should accept AlphaNumeric values , ie - user can enter the name of a system or IP address of a system. I have added 2 validations for this scenario. While clicking JButton , first it will check for the format of the IP Address(to identify whether it is a name or IP address) using this pattern

"^([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})$".

If it is an IP address, then it will check whether it is a valid IP address or not using this pattern

"^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"

and display a message - valid or not.

Problem - If I enter an invalid IP address with proper format like -

299.143.154.167

the first validation is not getting successful.

    import java.awt.Color;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;

    public class RegexDemo {
        public static String regExToCheckIPAddress = "^([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})$";
        public static Pattern pattern1;
        public static Matcher matcher1;
        public static String regExToCheckValidIPAddress = "^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
                                                          "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
                                                          "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
                                                          "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
        public static Pattern pattern2;
        public static Matcher matcher2;

        public static void main(String[] args) {
            JFrame frame = new JFrame("IP Address validation");
            JPanel panel = new JPanel();
            JTextField txtField = new JTextField();
            JButton btn = new JButton("Validate");
            JLabel lbl = new JLabel();
            lbl.setFont(new Font("Arial", Font.PLAIN, 12));
            lbl.setForeground(Color.RED);

            panel.setBounds(5,5,350,250);
            panel.setLayout(null);

            txtField.setBounds(5,5,130,25);
            btn.setBounds(20,40,80,25);
            lbl.setBounds(145,5,100,25);

            panel.add(txtField);
            panel.add(btn);
            panel.add(lbl);

            frame.add(panel);
            frame.setSize(330,300);
            frame.setLayout(null);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);

            String text = txtField.getText().trim();

            pattern1 = Pattern.compile(regExToCheckIPAddress);
            matcher1 = pattern1.matcher(text);

            pattern2 = Pattern.compile(regExToCheckValidIPAddress);
            matcher2 = pattern2.matcher(text);

            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    if (matcher1.matches()) {
                        if (!matcher2.matches()) {
                            lbl.setText("Not a valid IP Address");
                        } else {
                            lbl.setText("Valid IP Address");
                        }
                    } else {
                        lbl.setText("Not an IP Address");
                    }
                }
            });
        }
    }

If I enter a alphanumeric name , it should display 'Not an IP Address' , if I enter '299.143.154.167' , it should display 'Not a valid IP Address' , if '234.143.154.167' it should display 'Valid IP Address' .

This code is working fine, if I dont use Swing related components (without UI). Can any one help me on this?

The problem is you are using the default text of the text field. You need to get the text when the button is pressed. After getting the text you need to create matchers.

You need to get the text and create matchers with the new text in actionPerformed method like this:

    public void actionPerformed(ActionEvent ae) {
        String text = txtField.getText().trim();
        matcher1 = pattern1.matcher(text);
        matcher2 = pattern2.matcher(text);
        if (matcher1.matches()) {
            if (!matcher2.matches()) {
                lbl.setText("Not a valid IP Address");
            } else {
                lbl.setText("Valid IP Address");
            }
        } else {
            lbl.setText("Not an IP Address");
        }
    }

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