【问题标题】:How I can make the input control verify an email address is of valid form?如何使输入控件验证电子邮件地址的格式有效?
【发布时间】:2016-08-23 04:43:29
【问题描述】:

我有一个表格,其中要填写的字段是我要控制的邮件,以便输入地址与标准邮件一致

responsableTechnique.setMail(mailResponsable.getText());

【问题讨论】:

标签: java swing inputverifier


【解决方案1】:

您可以使用正则表达式,例如:

public static void main(String[]args){
    String regex = "^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)+$";

    Pattern.matches(regex,"test@test.fr"); //True
    Pattern.matches(regex,"test.test@test.fr"); //True
    Pattern.matches(regex,"testtest.fr"); //False
    Pattern.matches(regex,"test.test@testfr"); //False
    Pattern.matches(regex,"test@test."); //False
    Pattern.matches(regex,"test@te@st.fr"); //False

}

所以对于你的例子:

String regex = "^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)+$";
    String mail = mailResponsable.getText();

    if(Pattern.matches(regex,mail)) responsableTechnique.setMail(mail);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 2022-08-21
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    相关资源
    最近更新 更多