Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6176261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:04:08+00:00 2026-05-24T00:04:08+00:00

All, I’m attempting to write what should be simple validation for a Struts2 action.

  • 0

All,
I’m attempting to write what should be simple validation for a Struts2 action. I have a field that must contain 0 or a positive integer, so I’m attempting to use the Struts2 built in regex validator to accomplish this. The regex that I’m using is ‘^\d*$’, and I’ve tested that outside of struts2 and believe it should meet my needs. (e.g. it matches against ’23’, but not ‘abc’ or ‘-5’).

However, when I use that regex pattern in Struts2, it fails to give me a validation error for negative numbers.

Here’s my struts2 validation xml snippet:

<field name="editVO.capability.operationalQty">
        <field-validator type="regex">
            <param name="expression"><![CDATA[^\d*$]]></param>
         <message key="errors.number"/>
         </field-validator>
</field>

Here are some of the results I’m seeing when unit testing this validation:

Input  Validation Passes?  Expected Result?
23          Yes            Yes 
abc         No             Yes
-5          Yes            No 
%5          No             Yes 
5-          No             Yes 
a5          No             Yes

As you can see from the results above, when I run my unit test (or test through the app), I get an error message (as expected) when I enter ‘abc’ or ‘5-‘, but ‘-5’ does NOT trigger a validation failure. I have no idea why it’s allowing the ‘-‘ character through if it’s the first character.

I’d appreciate any Struts2 related regex tips; please note that I believe the error here is
somehow related specifically to Struts2 and how it handles regex, and not simply a regex-only issue. (fwiw – I’m getting the same issue when I try this pattern too: ‘^[0-9]*$’).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:04:10+00:00Added an answer on May 24, 2026 at 12:04 am
    <field-validator type="regex">
    

    This is a field validator, it requires that the field of the action already be set for it to work, it will not work against the request. It’s functionality is provided by the validation interceptor.

    For confirmation of this see the source (It really isn’t that bad):

    The validation interceptor is defined in struts-default.xml as such:

    <interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>
    

    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor

    The AnnotationValidationInterceptor extends com.opensymphony.xwork2.validator.ValidationInterceptor

    It is from the opening javadoc for ValidationInterceptor we get the following:

    /**
     * <!-- START SNIPPET: description -->
     *
     * This interceptor runs the action through the standard validation framework, which in turn checks the action against
     * any validation rules (found in files such as <i>ActionClass-validation.xml</i>) and adds field-level and action-level
     * error messages (provided that the action implements {@link com.opensymphony.xwork2.ValidationAware}). This interceptor
     * is often one of the last (or second to last) interceptors applied in a stack, as it assumes that all values have
     * already been set on the action.
    

    And that pretty much sums it up.

    I think your test cases are explained by type conversion alone. I suspect that params is removing the field error, it would not make any sense for validation errors to be kept if the field value has been reset.

    In conclusion: There is nothing wrong with Struts2 regex validation, at least when used in the intended way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All, I currently have two projects that are under SourceSafe that I am unable
All, I have some script tags that are not working in Wordpress. If I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
All I want is to make a simple user password login. I have put
All, I have an application that needs to attach and detach multiple SQL databases
All too often I want a WPF slider that behaves like the System.Windows.Forms.TrackBar of
I have just tried to save a simple *.rtf file with some websites and
All the guys probably will recommend that I read the follow previously question in
All programs that I develop utilize the default Windows Design template: Besides from changing
All, Say I have the following bit of code: select: function(start, end, allDay) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.