• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

struts integer validator

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the simplest way to validate that a user's input is an integer if the underlying data type in the form is int?
My problem is that I have a text field for entering integer values in which a user might accidentally type letters or floating point numbers. Unfortunately, it seems the form automatically sets the value to 0 if the input is invalid. Thus, the struts validator sees 0 instead of the user's actual input, which might have been "afd.04964."
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the exactly the same problem. Is there anyone have any ideas? Struts don't validate request incomings but the populated forms.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the simplest (depens on your preference):

validation.xml:
add a mask dependency that checks on [0-9] (regex)

if you're more comfortable with java in stead of xml:
add validate() method in the actionform and there you can check if the variable does have chars in it.

In both cases you do need to activate the validator framework by setting it in the struts-config.

If you don't want to use validator framework:
throw error in the action when your variable is null or 0
 
Don't MAKE me come back there with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic