Multiple Regex @Pattern's for 1 Field?

前端 未结 4 509
天涯浪人
天涯浪人 2020-12-30 03:15

I was attempted to apply multiple @Pattern annotations to a single field:

@Pattern(regexp = \"(?=.*[0-9])\", message = \"Password must contain one digit.\")
         


        
4条回答
  •  执笔经年
    2020-12-30 03:35

    You can use the inner @List annotation of @Pattern:

    @Pattern.List({
        @Pattern(regexp = "(?=.*[0-9])", message = "Password must contain one digit."),
        @Pattern(regexp = "(?=.*[a-z])", message = "Password must contain one lowercase letter."),
        @Pattern(regexp = "(?=.*[A-Z])", message = "Password must contain one uppercase letter."),
        @Pattern(regexp = "(?=\\S+$)", message = "Password must contain no whitespace.")
    })
    private String password;
    

提交回复
热议问题