wood burning stoves 2.0*
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 Murach's Java Servlets and JSP this week in the Servlets 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: 1012

How about annotating your phone number field as follows:

Obviously you can replace the max length 8 to suit your needs.
subject: integer (not mandatory) field validation
Similar Threads
Spring MVC and Form Validation
valueChangelistener vs Javascript
getting data from the database with the non mandatory fields
Is use of reflection api encouraged?
Program action of a page dynamically depending on the link from where it is called