简体   繁体   English

Java Swing中的电话号码验证

[英]Phone number validation in java swing

I'm creating a form in java swing and I need to verify that the user has indeed entered phone number and not "abcde". 我正在用Java swing创建一个表单,我需要验证用户确实输入了电话号码,而不是“ abcde”。

Do I put the code in the Jbutton area or the text field area? 是否将代码放在Jbutton区域或文本字段区域中?

I'm looking to use a try/catch to throw an exception. 我正在寻找使用try / catch引发异常。 My specific question is where does the code go? 我的具体问题是代码在哪里?

I would recommend using a DocumentFilter , which would allow you to perform a real time filter as the user types. 我建议使用DocumentFilter ,它允许您在用户键入时执行实时过滤。

The following is a very basic example of filtering out unwanted characters and limit the length of how many characters can be entered 以下是一个非常基本的示例,可以过滤掉不需要的字符并限制可以输入的字符数的长度

public class PhoneNumberDocumentFilter extends DocumentFilter {

    protected static String[] VALID_VALUES = new String[]{"+", "-", " ", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};

    private int maxLength = 21;

    public PhoneNumberDocumentFilter(int maxLength) {
        this.maxLength = maxLength;
    }

    public PhoneNumberDocumentFilter() {
    }

    protected String filter(String text) {
        StringBuilder sb = new StringBuilder();
        List<String> validValues = Arrays.asList(VALID_VALUES);
        for (int index = 0; index < text.length(); index++) {
            String value = text.substring(index, index + 1);
            if (validValues.contains(value)) {
                sb.append(value);
            }
        }
        return sb.toString();
    }

    @Override
    public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {

        Document doc = fb.getDocument();
        StringBuilder sb = new StringBuilder();

        sb.append(doc.getText(0, doc.getLength()));
        sb.insert(offset, string);
        String value = filter(string);
        if (value.length() > 0) {
            if (maxLength > 0 && doc.getLength() + value.length() <= maxLength) {
                super.insertString(fb, offset, value, attr);
            }
        }
    }

    @Override
    public void replace(FilterBypass fb, int offset, int length, String text,
                    AttributeSet attrs) throws BadLocationException {

        Document doc = fb.getDocument();
        StringBuilder sb = new StringBuilder(2);
        sb.append(doc.getText(0, doc.getLength()));
        sb.replace(offset, offset + length, text);

        String value = filter(text);
        if (value.length() > 0) {
            if (sb.length() > maxLength) {
                length = sb.length() - maxLength;
                if (length > 0) {
                    value = value.substring(0, length);
                }
            }
            super.replace(fb, offset, length, value, attrs);
        }

    }
}

What it doesn't do is valid the format, I'll leave that up to you to figure out how you might do that 它没有做的是有效的格式,我将由您自己决定要怎么做

Whether the code should be put for JButton or JTextField that is coder's choice. 是否应将代码放在JButton还是JTextField上,这是编码人员的选择。 But in both ways you have to follow different ideas. 但是在两种方式中,您都必须遵循不同的想法。

1. In JButton : If you put your code inside JButton, it is easiest. 1.在JButton如果将代码放入JButton中,这是最简单的。 You may use simple try-catch. 您可以使用简单的try-catch。 But it's main drawback is your phone number validation will occur after clicking on that JButton. 但是它的主要缺点是您的电话号码验证将在单击该JButton之后进行。 But users can still provide wrong input and they are not notified unless they click on the JButton. 但是用户仍然可以提供错误的输入,除非单击JButton,否则不会通知他们。

2. Using DocumentFilter : You may want to send notice to users in real time, when they are typing on the text field. 2.使用DocumentFilter当用户在文本字段中键入内容时,您可能希望实时向他们发送通知。 In that case, you have to use DocumentFilter . 在这种情况下,您必须使用DocumentFilter Here is the official link of DocumentFilter from Oracle where the procedure is described clearly. 这是Oracle的DocumentFilter的官方链接 ,其中对该过程进行了清楚的描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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