File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Spring and the fly likes integer (not mandatory) field validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "integer (not mandatory) field validation" Watch "integer (not mandatory) field validation" New topic

integer (not mandatory) field validation

Maverick Schueler

Joined: Jun 18, 2013
Posts: 5
Spring MVC 3

I have a form with an integer form bean field (e.g. phoneNumber). This field is not mandatory. but I need to have it only digits (e.g. 0111234567890)
But whenever I keep the field blank, Spring treats it as String and throws exception.

Is there any way we can achieve this?
James Boswell

Joined: Nov 09, 2011
Posts: 1051

How about annotating your phone number field as follows:

Obviously you can replace the max length 8 to suit your needs.
I agree. Here's the link:
subject: integer (not mandatory) field validation
It's not a secret anymore!