• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to validate the length of a number?  RSS feed

 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I want to find the number of digits / length of a number while validating a field.

Currently I am usingWhen I try to use @Length or @Size I am running into exceptions. Later I discovered that they can only be used with strings. Is there another alternative or I have to use this technique only?

Thank you all in advance. Good day.
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to use the just the standard JSR-303 annotations then the answer is no. But I assume you are using the hibernate-validation library since you mentioned @Length. If you are using this library you may also use



That said you can also create your own custom annotations and validators very easily although in this case I would say that what is available is sufficient.
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And that is not a Spring question, so why post in the Spring forum?

But I am also not sure where to move this thread.

Mark
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,

I was thinking the same thing. I think it is a HIbernate Validator question so maybe the 'Other Open Source Projects' forum would make the most sense.
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about a brand new forum I just created specifically for JSR 303 - Validation. ;)

Mark
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice There have been quite a few questions regarding that lately.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i am using hibernate validator api. So I thing there is no annotation which can validate the length of a given number.

@NumberFormat takes a style and a pattern. Can I use some kind of regular expression in it?

And thanks Mark for creating a new forum.
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill Gorder wrote:If you want to use the just the standard JSR-303 annotations then the answer is no. But I assume you are using the hibernate-validation library since you mentioned @Length. If you are using this library you may also use



That said you can also create your own custom annotations and validators very easily although in this case I would say that what is available is sufficient.



NumberFormat is Spring annotation for formatting numbers not validating them. Did you see my previous post? You can use Range or if that does not suit you either you can write your own custom constraint, but in this case any kind of custom length validator you wrote to work on a number would be less efficient than those provided.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I changed it to @Range instead of using two annotations.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!