3

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.

1

2 Answers 2

4

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]+")   
Sign up to request clarification or add additional context in comments.

3 Comments

Note the javadoc for @Pattern: The annotated {@code CharSequence} must match the specified regular expression. It's useless on Long.
@Narendra No, an Integer or Long field doesn't match a regular expression.
It 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. :/
1

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.

4 Comments

I 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
Is there any way to provide custom messasg to the above error?
@Narendra No, because we aren't using validation with @Valid here.
@ 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

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.