Introduction
The approval date implies when the Referred Party has effectively approved their personality and finished the onboarding methods. You can responsively set a validator for a min, and max dates dependent on the ongoing date, albeit no default Angular validator at present exists. You'll have to make a custom validator. Import the DateValidator class into your part where the responsive structure is made, and apply the validator portrayed previously.
In this article we will understand date validator and how we can create one ourselves using jsp.
Date Validator 📅
The struts web application checks whether the entered date is inside the predefined range. Three boundaries characterized by the date validator are:
fieldName: It is utilized to determine the name of the field which is to be validated.
Min: This boundary is utilized to portray the base date permitted.
Max: This boundary is utilized to depict the most extreme date permitted.
Struts Bundled Validators - Date Example:
To get input from the client, make the index.jsp record:
The index.jsp page takes the contribution from the client. The client will enter the email, secret word, and the Date of Birth. Then, at that point, after tapping on the Register button, it will divert to the following asset. It has three textfield and one button on the structure with the connection to LoginAction class. 🧑💻
<validators>
<!-- Plain Validator syntax -->
<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 from ${min} and ${max}</message>
</validator>
</validators>
<validators>
<!-- Field Validator Syntax -->
<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 from ${min} and ${max}</message>
</field>
</field>
</validators>
1️⃣ Create index.jsp for Input
This jsp page makes a structure utilizing struts UI labels. It gets a name, secret word, and email id from the client.
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 Activity Class
This activity class acquires the ActionSupport type and supersedes the execute technique.
RegisterAction.java
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 Approval Record
Here, we are utilizing packaged validators to play out the approval.
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/2020</param>
<message>Date of Birth must be from ${min} to ${max}</message>
</field-validator>
</field>
</validators>
4️⃣ Create struts.xml
This xml record characterizes a different outcome 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 a View Part
It is a straightforward jsp record showing the data of the client.
Welcome.jsp
<%@ taglib uri="/struts-tags" prefix="s" %>
Date of Birth:<s:property value="dob"/>
Output 📤
To run the application,
- >Right-click on the undertaking
- > Click on the choice Run As
- > then select Run on Server.
It will show three names for entering the email: DOB, password, and the Register button.
Enter any email and enter the secret key whose length is somewhere between 6 and 10 and the date after 01/01/2002.
The mistake message will show because the date isn't inside the reach.
Again run the application. This time enter any email and enter a secret phrase whose length is more noteworthy than 6 and under 10. Enter the date inside the reach 01/01/1950 to 01/01/2002
As the email, DOB, and secret word are placed accurately, and executing strategy will return a triumph string. It will divert to page welcome.jsp and show a Welcome message.