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.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Narendra Pandey
  • 514
  • 4
  • 11
  • 26

2 Answers2

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]+")   
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
  • Note the javadoc for `@Pattern`: `The annotated {@code CharSequence} must match the specified regular expression.` It's useless on `Long`. – Sotirios Delimanolis Sep 17 '13 at 15:28
  • 1
    @Narendra No, an `Integer` or `Long` field doesn't match a regular expression. – Sotirios Delimanolis Sep 17 '13 at 15:30
  • 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. :/ – Narendra Pandey Sep 17 '13 at 15:31
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.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • 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 – Narendra Pandey Sep 17 '13 at 15:28
  • Is there any way to provide custom messasg to the above error? – Narendra Pandey Sep 17 '13 at 15:33
  • @Narendra No, because we aren't using validation with `@Valid` here. – Sotirios Delimanolis Sep 17 '13 at 15:34
  • @ 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 http://stackoverflow.com/questions/4082924/jsr-303-type-checking-before-binding I printed the BindingResult and tried few combination...and it worked – Narendra Pandey Sep 17 '13 at 16:09