aspose file tools*
The moose likes Validation -JSR303 and the fly likes How to validate the length of a number? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java JSRs » Validation -JSR303
Bookmark "How to validate the length of a number?" Watch "How to validate the length of a number?" New topic
Author

How to validate the length of a number?

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

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.


Love all, trust a few, do wrong to none.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

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.


[How To Ask Questions][Read before you PM me]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

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

Joined: Feb 05, 2001
Posts: 17249
    
    6

How about a brand new forum I just created specifically for JSR 303 - Validation. ;)

Mark
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

Nice There have been quite a few questions regarding that lately.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

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

Joined: Mar 07, 2010
Posts: 1632
    
    7

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

Joined: Sep 15, 2009
Posts: 800

Yes I changed it to @Range instead of using two annotations.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to validate the length of a number?
 
Similar Threads
Learning Project 2
How to find number of chinese characters?
Generate unique numbers from a table's primary key, to be used as an external reference.
help with creating random strings in an array
How to automatically classify data in a large database