DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Struts Mask Validation Rule Example

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 15, 12 · · Tutorial
Like (0)
Save
Tweet
42.68K Views

Join the DZone community and get the full member experience.

Join For Free

In this example you will see how to use the mask rule to restrict the user input. Here the userForm has two fields, one for the user name and the other for the phone number. We will restrict the user to enter only alphabets in the user name field and only numbers in the phone number field. The struts-config.xml file has the following entry for userForm.

<form-beans>
    <form-bean name="userForm" type="org.apache.struts.validator.DynaValidatorForm">
        <form-property name="userName" type="java.lang.String" />
        <form-property name="phoneNumber" type="java.lang.String" />
    </form-bean>
</form-beans>

The validation.xml file contains the following codes.

<form name="userForm">
    <field property="userName" depends="required,mask">
        <msg name="mask" key="userForm.username.mask" />
        <arg key="userForm.username"/>
        <var>
            <var-name>mask</var-name>
            <var-value>^[a-zA-Z]*$</var-value>
        </var>
    </field>
    <field property="phoneNumber" depends="required,mask">
        <msg name="mask" key="userForm.phoneNumber.mask" />
        <arg key="userForm.phoneNumber"/>
        <var>
            <var-name>mask</var-name>
            <var-value>^[0-9]*$</var-value>
        </var>
    </field>
</form>

The required rule is used to ensure that the value is entered by the user and the mask rule is used to restrict the user from entering invalid data.

The following messages should be configured in the ApplicationResource.properties file. If invalid data is entered by the user, then these values will be used to display the appropriate error message.

userForm.username.mask = {0} should contain only alphabets.
userForm.phoneNumber.mask = {0} should contain only numbers.
userForm.username = User Name
userForm.phoneNumber = Phone Number

The "userForm.username.mask" value is used to display the error message when an invalid user name is entered.

On running this sample mask validation rule example the following page is displayed. The user needs to enter a valid user name and phone number to register successfully.

When the user clicks the submit button without entering the user name and the phone number the following error messages are displayed.

When the user enter a invalid user name and invalid phone number the following error messages are displayed.

You can download the source code of the mask validation example by clicking on the Download link below.

Source: Download

Source + Lib: Download

Download Error message Data (computing) Links

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Caching Across Layers in Software Architecture
  • What Emerging Technologies Make Data Centers More Energy Efficient?
  • Are All Kubernetes Ingresses the Same?
  • To Shift Right, You Need Observability

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo