-2
class SingIn {

    private String login;

    private String password;

    public SingIn(String login, String password) {
        this.login = login;
        this.password = password;
    }

    public String getLogin() {
        return login;
    }

    public String getPassword() {
        return password;
    }
}

登录属性可以是电子邮件(模式“.+@.+”)或电话号码(模式“\+\d+”)。

是否可以使用 javax 注释以某种方式验证登录属性?

4

1 回答 1

1

是的,可以通过@Pattern用 logical 组合 2 个注释来实现OR

@ConstraintComposition(OR)
@Pattern(regexp = ".+@.+")
@Pattern(regexp = "\+\d+")
@ReportAsSingleViolation
@Target({ METHOD, FIELD })
@Retention(RUNTIME)
@Constraint(validatedBy = { })
public @interface EmailOrPhone {

另请参阅Hibernate Validator 文档中的约束的布尔组合主题。

于 2017-08-24T13:41:43.047 回答