Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 保存前验证电话号码_Java - Fatal编程技术网

Java 保存前验证电话号码

Java 保存前验证电话号码,java,Java,我这里有一个验证电话号码的代码 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}$";

我这里有一个验证电话号码的代码

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");
        }
    }
}
如何将此代码放入SAVEBUTTON操作中

private void saveButton3ActionPerformed(java.awt.event.ActionEvent evt) {...}
防止用户保存无效的电话号码格式


谢谢大家!

假设您正在类
公共类清单扩展javax.swing.JFrame
(如您在评论中所指定)中编写代码,我将编写一个
ActionListener
来处理按钮单击事件

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());
        ...
    }
}
或者(如有人强调的)可以使用匿名类,从而将其简化为以下内容:

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
             ...
          }
        });
        ...
    }
}

下面是实现这一点的简单方法

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);
    }
}

根据你提供的信息,很难回答你的问题。但是,您需要某种方法从您的操作中检索电话号码,然后通过某种方法批准或不批准电话号码,以便应用程序可以保存该电话号码,或者在出现问题时提醒用户。对不起。我是java新手。我想做的就是防止用户保存无效的电话号码格式。如果你觉得我的答案有用,你能接受吗?从那以后已经三年了……:)您也可以使用匿名类。Hello@FSp感谢您回答我的问题,但我想澄清一下,这个类ButtonListener是否实现了ActionListener{public void actionPerformed(ActionEvent ae){//此处的代码..}应该放在公共类清单extensed javax.swing.JFrame内,并放在私有void saveButton3ActionPerformed(java.awt.event.ActionEvent evt)外{…}对不起,我是java新手:(@mix您的问题的简短答案是:是的。我提供了一个更详细的示例,希望它再次澄清Hello。感谢您为我这样的初学者提供了清晰的解释:)我只想问一下是currPhoneNumber(我的电话号码字段的名称?)@在我的示例中,它是
currPhoneNumber
只是一个
字符串
。如果将其设置为
TextField
,则必须将其内容读取为
currPhoneNumber.getText()
(但在概念上是等效的)您好@john谢谢您给我提供的答案。我只是想澄清一下,我似乎是java新手,我不能很好地理解。您上面显示的代码,我应该将它们放在私有的void saveButton3ActionPerformed(java.awt.event.ActionEvent evt){…}