5

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

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Pattern is not getting evaluated for me.
3

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.