Get all updates of Java4s
Follow Us: Email Newsletter

Struts2 Validation Rules and Their Syntax

Struts » On Oct 24, 2011 { 2 Comments } By Sivateja

For every rule there is a class, all classes here are implements Validator interface

  • required
  • requiredstring
  • stringlength
  • int
  • double
  • date
  • email
  • url
  • fieldexpression
  • regex
  • few other rules are there but light..!!

For each rule given, there is an implementation class exist for it as told above.

For ‘required’ rule the class name is RequiredFieldValidator, for ‘int’ the class name is IntRangeFieldValidator

If we want to create our own validator class then our class should also implements validator interface.  Validator interface is given in com.opensymphony.xwork2.validators.*.


This rule verify whether user entered value is null or not, here we cannot pass any parameters


This will validates whether the input entered is a valid string or not we can pass a parameter called trim to this rule.

Trim will removes extra space from left and right sides of given word


This rule validates whether the given input is with in the given range of characters or not.  This rule is the combination of both min, max length rules.

To this rule we can pass either both parameters min length and max length or any one of the paramenters



This rule verifies whether the given input value is with in the given range of integer or not, To this rule we need to pass min, max parameters


Note: Actually ‘double‘ also same as int, but we need to pass the parameters minInclusive,maxInclusive


This will validate whether the given input date is with in the range of the dates or not, we need to pass min, max parameters to this rule.

When we enter the date values then the format to be followed is dd-mm-yyyy


And email, url are simple just like required, we no need to pass any parameters hope you are good.


In struts 1.x, with validator frame work we can do only per field validations, i mean we can validate field by field.  But here in struts 2,x we can do the between field validations also with this fieldexpression rule.

Actually if we want to do between fields in struts 1.x, we must do manually but here we can do this via xwork validator framework which supports both per-field and between filed validations also.

To work with this rule we must pass one parameter named ‘expression

Between field means, we can compare the current entered value with any one of above field.


Note: here #myFatherBirth is other field name

You Might Also Like

  ♦ About Author ♦

Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.


2 Responses to “Struts2 Validation Rules and Their Syntax”
  1. pramod says:

    How I can find Action class object in servlet

  2. Vijaya says:

    I am trying to use “fieldexpression” tag as you mentioned. I am working on Struts 2.0.11.

    // validator name: fieldexpression
    if (form.elements[‘endDateString’]) {
    field = form.elements[‘endDateString’];
    var error = “Your Birth date must be less then your father dob”;

    I am getting but the If condition is not added.
    I have added below statement in XML file

    Your Birth date must be less then your father dob


By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2016 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.