Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how check string is integer or not by using spring annotations?

**i want test user input is a alpha-numeric or integer by 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;
like image 947
Suraj Avatar asked Aug 27 '14 02:08

Suraj


2 Answers

I ran into a similar problem and used the @Pattern annotation to compare against a regular expression.

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

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

like image 183
randal4 Avatar answered Oct 31 '22 18:10

randal4


The @NumberFormat annotation only applies to subclasses of java.lang.Number (Integer, Float, Double, BigDecimal, etc.); therefore, it won't work with Strings. For this, you may have to create a custom annotation that validates the String only contains numbers.

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

like image 36
adchilds Avatar answered Oct 31 '22 16:10

adchilds