Multiple Regex @Pattern's for 1 Field?

爷,独闯天下 提交于 2019-11-29 07:59:57

问题


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

@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;

However, I cannot do this. I want individual messages per violated regex constraint on the password field. Is this possible?

My alternative is to use JSF 2.0 f:validatorRegex tags.


回答1:


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;



回答2:


Gunnar's solution won't work for me... '.+' in his regexp seem to be missing. However, i'm using Michal's patternList and it works like a charm for me. (Play 2.3.x / Ebean-ORM)

@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 upper letter."),
        @Pattern(regexp = "(?=.*[!@#$%^&*+=?-_()/\"\\.,<>~`;:]).+", message ="Password must contain one special character."),
        @Pattern(regexp = "(?=\\S+$).+", message = "Password must contain no whitespace.")
})
@Constraints.Required()
public String password1;



回答3:


You might want to look into constraint composition. You would build one annotation per password constraint composed from the @Pattern annotation, and then finally build one composed annotation using the previously defined four. This would require no extra java code.

http://docs.jboss.org/hibernate/validator/5.0/reference/en-US/html_single/#validator-customconstraints-compound

Otherwise, build (with accompanying java code) a custom annotation called @PasswordValidator.

I tend to like composed validators, as it makes the purpose of the validation annotations clear on their purpose, instead of having many disjoint annotations.




回答4:


I modified Gunnar answer and write composite constraint and this now seems to work correctly on 4 unit tests:

@NotNull
@Size(min=6, max=45)
@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 = "(?=.*[!@#$%^&*+=?-_()/\"\\.,<>~`;:]).+", message ="Password must contain one special character."),
    @Pattern(regexp = "(?=\\S+$).+", message = "Password must contain no whitespace.")
})
@Constraint(validatedBy = {}) // constraints composition
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface StrongPassword {

     String message() default "Password doesn't match bean validation constraints.";
     Class<?>[] groups() default {};
     Class<? extends Payload>[] payload() default {};
}


来源:https://stackoverflow.com/questions/16225015/multiple-regex-patterns-for-1-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!