How to restrict a user to enter numerics in a Integer field with help of annotations?
Solution 1
Hibernate
@Range - Check if the value is between min and max
Example
@Range(min=1, max=1000)
@Pattern - Check if the property match the regular expression given a match flag @Pattern(regex="regexp", flag=) or @Patterns( {@Pattern(...)} )
Example
@Pattern(regex = "[0-9]+")
Spring
@RegExp - Check if the property match the regular expression given a match flag
Example
@RegExp("[0-9]+")
Solution 2
This isn't something that would be done on the validation level.
When you enter something in a form and you submit that form, the browser will send an HTTP request with your <input>
fields serialized and sent as request parameters. Spring then, based on the data type of your bean's field, tries to convert from a String
request parameter value to a Long
type. If it can't do that because the String is not a Long
, it will throw exceptions (NumberFormatException
) and respond with a 400 error code.
You can validate this on the (HTML5) client side with
<input name="userId" type="number">
Or use
<input type="text" pattern="\d*" />
if you don't want decimal numbers.
Comments
-
Narendra Pandey almost 2 years
I need to restrain a user to enter numeric values in a jsp field.On failure it should give relevant message.This is my current declaration:
@NotNull @Column(value="userId") private Long userId;
I need to know what more annotations do i need to add, to get my desired result without changing data type of the field.
-
axtavt over 10 years
-
-
Narendra Pandey over 10 yearsI tried it but sill it prints error on screen: Failed to convert property value of type java.lang.String to required type java.lang.Long for property phoneNumber; nested exception is java.lang.NumberFormatException: For input string: "aa" ... I want to provide a personal message for it.How's that possible.I tried using @Numberformat annotation
-
Sotirios Delimanolis over 10 yearsNote the javadoc for
@Pattern
:The annotated {@code CharSequence} must match the specified regular expression.
It's useless onLong
. -
Sotirios Delimanolis over 10 years@Narendra No, an
Integer
orLong
field doesn't match a regular expression. -
Narendra Pandey over 10 yearsIt works like charm on String fields..But i want to stick on taking my field as Long only.It will be a last resort were i took values in String field and later dumping values in Db as Long after exacting there values from String. :/
-
Narendra Pandey over 10 yearsIs there any way to provide custom messasg to the above error?
-
Sotirios Delimanolis over 10 years@Narendra No, because we aren't using validation with
@Valid
here. -
Narendra Pandey over 10 years@ Sotirios Delimanolis--Thanks for your replies...It worked somehow after getting to know the proper error message to use in proerty file...previously i was using 'Pojo.attribute[property]="message" ' in my message file....but for such issues... i found this link quiet helpful stackoverflow.com/questions/4082924/… I printed the BindingResult and tried few combination...and it worked