Search code examples
validationlifecyclestruts-1

Struts 1 Textbox to integer validation


In struts 1 if you try to bind a html:text field directly to an integer in the ActionForm then there isn't a chance to validate it correctly when the user enters a non-numberic value.

If the user enters a non-numberic value, then the integer value is always parsed as 0 before it reaches the validate method.

Is there any way supported way that struts provides to handle this situation? Or do I need to always bind to a String first and then parse into an integer later on?


Solution

  • If I am not mistaken, any thing that comes from the UI is a string, even though you have the variable defined as an integer in your form.

    So my suggestion would be to declare the variable as string and parse it according to your need.