【问题标题】:Hibernate email pattern fails for specified domains指定域的休眠电子邮件模式失败
【发布时间】:2017-04-22 19:33:27
【问题描述】:

我使用@Pattern 验证@Email,将电子邮件地址缩小到三个域和name.lastname 用户名。但是我的代码失败并且每次都显示消息。我究竟做错了什么?

@Embeddable
public class Contact
{
    @NotNull
    @NotEmpty
    private String              firstname;
    @NotNull
    @NotEmpty
    private String              lastname;
    @NotNull
    @NotEmpty
    @Email
    @Pattern.List({ @Pattern(regexp = ".+(@domain1.com|@domain-2.com|@dom-ain-three.com)", message = "Wrong email address") })
    private String              email;
}

【问题讨论】:

    标签: java hibernate hibernate-validator


    【解决方案1】:

    正确的代码是:

    @Embeddable
    public class Contact
    {
        @NotNull
        @NotEmpty
        private String              firstname;
        @NotNull
        @NotEmpty
        private String              lastname;
        @NotNull
        @NotEmpty
        @Email
        @Pattern.List({ @Pattern(regexp = ".+((@domain1\\.com)|(@domain-2\\.com)|(@dom-ain-three\\.com))", message = "Wrong email address") })
        private String              email;
    }
    

    转义点字符(表示除换行符以外的任何字符)修复了正则表达式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多