简体   繁体   中英

validate phone number before saving

I have here a code to validate a phone number

public class ValidatePhoneNumber {

    public static void main(String[] argv) {

        String phoneNumber = "1-(80..2)-321-0361";
        System.out.println(phoneNumber.length());
        String regex = "^\\+?[0-9. ()-]{10,25}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(phoneNumber);

        if (matcher.matches()) {
            System.out.println("Phone Number Valid");
        } else {
            System.out.println("Phone Number must be in the form XXX-XXXXXXX");
        }
    }
}

How can i put this code inside the SAVEBUTTON ACTION?

private void saveButton3ActionPerformed(java.awt.event.ActionEvent evt) {...}

to prevent the user for saving invalid phone number format

THANK YOU!

Supposing you are writing your code within a class public class Inventory extends javax.swing.JFrame (as you specified in your comment), I would write an ActionListener to handle the button click event

public class Inventory extends javax.swing.JFrame
    private String currPhoneNumber; // it may contains something like "1-(80..2)-321-0361"

    ...

    // this code can be *within* your class, but *outside* any method declaration
    class ButtonListener implements ActionListener {
      public void actionPerformed(ActionEvent ae) {
        System.out.println(currPhoneNumber.length());
        String regex = "^\\+?[0-9. ()-]{10,25}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(currPhoneNumber);
      }

      if (matcher.matches()) {
        System.out.println("Phone Number Valid");
      } else {
        System.out.println("Phone Number must be in the form XXX-XXXXXXX");

        // throw an exception or do something that will prevent the data to be 
        // saved (what to do here really depends on the application you are writing)
      }

    }

    ...

    private void createMyFancyInterface(...) { 
        ...
        JButton source = new JButton("Do something");
        source.addActionListener(new ButtonListener());
        ...
    }
}

Alternatively (as someone underlined) you can use anonymous classes, thus reducing it to the following:

public class Inventory extends javax.swing.JFrame
    private String currPhoneNumber; // it may contains something like "1-(80..2)-321-0361"

    ...

    // this code can be *within* your class, but *outside* any method declaration
    class ButtonListener implements ActionListener {
      public void actionPerformed(ActionEvent ae) {
        System.out.println(currPhoneNumber.length());
        String regex = "^\\+?[0-9. ()-]{10,25}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(currPhoneNumber);
      }

      if (matcher.matches()) {
        System.out.println("Phone Number Valid");
      } else {
        System.out.println("Phone Number must be in the form XXX-XXXXXXX");

        // throw an exception or do something that will prevent the data to be 
        // saved (what to do here really depends on the application you are writing)
      }

    }

    ...

    private void createMyFancyInterface(...) { 
        ...
        JButton source = new JButton("Do something");
        source.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
             // the same code as ButtonListener.actionPerformed above
             ...
          }
        });
        ...
    }
}

Here is the simple way to do this.

class JavaApplication extends JFrame implements ActionListener {

    JTextArea area;
    JButton button;
    JTextField box;
    JPanel panel;

    public JavaApplication19() {
        panel = new JPanel();
        panel = (JPanel) getContentPane();
        panel.setLayout(new FlowLayout());
        area = new JTextArea(26, 30);
        box = new JTextField(30);
        button = new JButton("Submit");
        button.addActionListener(this);
        panel.add(area);
        panel.add(button);
        panel.add(box);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String phoneNumber = area.getText();
        System.out.println(phoneNumber);
        String regex = "^\\+?[0-9. ()-]{10,25}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(phoneNumber);

        if (matcher.matches()) {
            box.setText("Phone Number Valid");
        } else {
            box.setText("Phone Number must be in the form XXX-XXXXXXX");
        }
    }

    public static void main(String[] args) {
        JavaApplication pad = new JavaApplication();
        pad.setSize(500, 500);
        pad.setVisible(true);
    }
}

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