How is the validation string whole or not using spring annotations?

** I want the test user input to be alphanumeric or integer using spring form annotations **

i have also tried this code.

      

 @NotEmpty
        @NumberFormat(style = Style.NUMBER)
        @Length(min =11,max =11)
        @Column(name="abc")
        private String xyz;

      

+3


source to share


2 answers


@NumberFormat

annotation only applies to subclasses of java.lang.Number ( Integer , Float , Double , BigDecimal , etc.); so it won't work with strings. To do this, you may need to create a custom annotation that checks that the string contains only numbers.



Take a look at the following tutorial; it goes into great detail on how to create custom annotations and use them for validation with Spring: http://softwarecave.org/2014/03/27/custom-bean-validation-constraints/

+2


source


I faced a similar problem and used annotation @Pattern

to compare against regex.

@Pattern(regexp="^(0|[1-9][0-9]*)$")

      



The annotation is part of the package javax.validation.constraints

.

+2


source







All Articles