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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 973

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
Similar Threads
Spring MVC and Form Validation
valueChangelistener vs Javascript
Program action of a page dynamically depending on the link from where it is called
Is use of reflection api encouraged?
getting data from the database with the non mandatory fields