The date validator checks that given date is witin the specified range.
Parameters of date validator
There is 3 parameters defined for date validator.
Parameter | Description |
fieldName | specifies the field name that is to be validated. It is required in Plain-Validator only. |
min | specifies the minimum range. It is ignored bydefault. |
max | specifies the maximum range. It is ignored bydefault. |
Example of date validator
- <validators>
-
- <validator type="date">
- <param name="fieldName">dob</param>
- <param name="min">01/01/1980</param>
- <param name="max">01/01/2010</param>
- <message>Date of Birth must be within ${min} and ${max}</message>
- </validator>
- </validators>
- <validators>
-
- <field name="dob">
- <field-validator type="date">
- <param name="min">01/01/1980</param>
- <param name="max">01/01/2010</param>
- <message>Date of Birth must be within ${min} and ${max}</message>
- </field>
- </field>
-
- </validators>
Full example of date validator
1) Create index.jsp for input
This jsp page creates a form using struts UI tags. It receives name, password and email id from the user.
index.jsp
- <%@ taglib uri="/struts-tags" prefix="s" %>
-
- <s:form action="register">
- <s:textfield name="dob" label="Date of Birth"></s:textfield>
- <s:submit value="register"></s:submit>
- </s:form>
2) Create the action class
This action class inherits the ActionSupport class and overrides the execute method.
RegisterAction.java
- package com.javatpoint;
-
- import java.util.Date;
-
- import com.opensymphony.xwork2.ActionSupport;
-
- public class Register extends ActionSupport{
- private Date dob;
-
-
- public Date getDob() {
- return dob;
- }
-
-
- public void setDob(Date dob) {
- this.dob = dob;
- }
-
-
- public String execute(){
- return "success";
- }
-
- }
3) Create the validation file
Here, we are using bundled validators to perform the validation.
Register-validation.xml
- <?xml version="1.0" encoding="UTF-8"?>
-
- <!DOCTYPE validators PUBLIC
- "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
- "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
-
- <validators>
-
- <field name="dob">
- <field-validator type="date">
- <param name="min">01/01/1950</param>
- <param name="max">01/01/2010</param>
-
- <message>Date of Birth must be between ${min} to ${max}</message>
- </field-validator>
- </field>
-
-
- </validators>
4) Create struts.xml
This xml file defines an extra result by the name input, and an interceptor jsonValidatorWorkflowStack.
struts.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
- <struts>
-
- <package name="default" extends="struts-default">
- <action name="register" class="com.javatpoint.Register">
- <result name="input">index.jsp</result>
- <result>welcome.jsp</result>
- </action>
- </package>
-
- </struts>
-
-
5) Create view component
It is the simple jsp file displaying the information of the user.
welcome.jsp
- <%@ taglib uri="/struts-tags" prefix="s" %>
-
- Date of Birth:<s:property value="dob"/>