This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Struts and the fly likes Struts 2 Integer Validation not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 2 Integer Validation not working " Watch "Struts 2 Integer Validation not working " New topic

Struts 2 Integer Validation not working

Priyanka Dandekar
Ranch Hand

Joined: Aug 06, 2008
Posts: 52
Experts, I am trying to add Integer validation to the my Struts 2 application.
Here is what I have done

<validator type="int">
<param name="fieldName">zipCode</param>
<message>Zip Code needs to be a valid number </message>

I have already set validate="true" in the <s:form> tag
This is successfully generating the java script code at my JSP. But when I see the generated code. The validation code for my field is hacing code

Not sure why this code is coming
if (false || false) {

Due to this it will not do validation for this field.

Any clue? Am I doing something wrong here? Please help.

Priya - Mockup Tool | Java | Struts2 | Lucene
Sachin Joshi
Ranch Hand

Joined: Aug 06, 2008
Posts: 83

looks like the Struts 2 "int" type validator does the range check. It uses IntRangeFieldValidator which is checking for integer range only.

I think its still beta so they may be fixing some issues. It should be working as you are expecting but if this does not work then have a workaround by using regular expression for number validation in your case it should work fine as you have zip code which need not be checked for the range.

Here is a Example Code which demonstrate how you can use regular expression validation for number validation in Struts 2.

Web Development Tips and Tutorials - By Sachin
I agree. Here's the link:
subject: Struts 2 Integer Validation not working
Similar Threads
problem in field validation
Struts2 very simple validation doesn't work
Adding more than one validation to form field
jsp form validation using bean with int
Struts 2 Validation