GWT 电话号码屏蔽

发布于 2024-11-14 03:38:12 字数 213 浏览 4 评论 0原文

有谁知道如何创建执行电话号码格式屏蔽的字段,如下所示 (___) ___-____:
http://www.smartclient.com/smartgwt/showcase/#form_masking

Does anyone know how to go about creating field that would perform telephone number format masking, like here (___) ___-____:
http://www.smartclient.com/smartgwt/showcase/#form_masking

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咆哮 2024-11-21 03:38:12

更好的方法是让用户输入他们想要的任何内容:“789-555-1234”或“(789) 555-1234”或“7895551234”,然后当字段失去焦点时决定他们输入的内容是否可以是电话数字。如果是这样,您可以将其重新格式化为“(789) 555-1234”。关于如何使用正则表达式执行此类操作,有几个相关的问题;只需确保您的正则表达式接受您要更改用户输入的格式,否则编辑起来会很烦人。

举个例子,看看当您在 Microsoft 标准页面设置对话框的左边距字段中键入“.5”时会发生什么:当您按 Tab 键时,它会将其更改为“0.5”。

更新:这是 GWT 中的示例代码来说明。对于此示例,假设有一个名为“phoneContainer”的元素来放入文本框。GWT 不会为您提供完整的 java.util.regex 包,但它提供了足够的功能来执行此操作:

private void reformatPhone(TextBox phoneField) {
    String text = phoneField.getText();
    text = text.replaceAll("\\D+", "");
    if (text.length() == 10) {
        phoneField.setText("(" + text.substring(0, 3) + ") " + text.substring(3, 6) + "-" + text.substring(6, 10));
    }
}


public void onModuleLoad() {
    final TextBox phoneField = new TextBox();

    RootPanel.get("phoneContainer").add(phoneField);
    phoneField.addBlurHandler(new BlurHandler(){
        public void onBlur(BlurEvent event) {
            reformatPhone(phoneField);
        }
    });
}

A better approach would be to let the user type whatever they want: "789-555-1234" or "(789) 555-1234" or "7895551234" and then when the field loses focus decide if what they typed can be a phone number. If so you can reformat it as "(789) 555-1234". There are several related questions about how to do that sort of thing with regular expressions; just be sure your regex accepts the format you're changing the user's input to, otherwise it will be really annoying to edit.

As an example, look what happens when you type ".5" into the left margin field in Microsoft's standard page setup dialog: when you tab out it changes it to "0.5".

UPDATE: Here's sample code in GWT to illustrate. For the sake of this example, assume there's an element called "phoneContainer" to put the text box in. GWT doesn't give you the full java.util.regex package, but it gives enough to do this:

private void reformatPhone(TextBox phoneField) {
    String text = phoneField.getText();
    text = text.replaceAll("\\D+", "");
    if (text.length() == 10) {
        phoneField.setText("(" + text.substring(0, 3) + ") " + text.substring(3, 6) + "-" + text.substring(6, 10));
    }
}


public void onModuleLoad() {
    final TextBox phoneField = new TextBox();

    RootPanel.get("phoneContainer").add(phoneField);
    phoneField.addBlurHandler(new BlurHandler(){
        public void onBlur(BlurEvent event) {
            reformatPhone(phoneField);
        }
    });
}
巡山小妖精 2024-11-21 03:38:12

看起来您想要创建自己的小部件来扩展 GWT 输入框,并将默认值设置为您想要的掩码。然后处理 onKeypress 事件并根据需要更新字段(确保将光标位置设置到正确的位置)。

It looks like you'd want to create your own widget that extends the GWT input box and has a default value set to the mask you want. Then you handle the onKeypress event and update the field as needed (making sure to set the cursor position to the correct location).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文