Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Date Validator 📅
2.1.
Struts Bundled Validators - Date Example:
2.2.
1️⃣ Create index.jsp for Input
2.3.
2️⃣ Create the Activity Class
2.4.
3️⃣ Create the Approval Record
2.5.
4️⃣ Create struts.xml
2.6.
5️⃣ Create a View Part
2.7.
Output 📤
3.
Frequently Asked Questions
3.1.
What is meant by date validation?
3.2.
What's a substantial date design?
3.3.
How would you approve a structure?
3.4.
How would I approve a date in python?
3.5.
How would you add a validator to receptive structure?
3.6.
How would you approve a timestamp?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

Date Validator

Author Adya Tiwari
0 upvote

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 CN

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>
  
struts

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";  
}  
 
}
CN gif

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.

date op

 

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.

date validator
Output CN

Frequently Asked Questions

What is meant by date validation?

Date validtion implies the date when the Referred Party has effectively approved their character and wholly finished the onboarding methods.

What's a substantial date design?

MM/DD/YY. Month-Day-Year with driving zeros (02/17/2009) 2. DD/MM/YY. Day-Month-Year with going zeros.

How would you approve a structure?

Fundamental Validation − First of all, the structure should be checked to ensure every one of the obligatory fields is filled in.

How would I approve a date in python?

The date approval you need to accomplish in python will generally rely upon the arrangement of the date you have. The strptime capability from the DateTime library can be utilized to parse strings to dates/times.

How would you add a validator to receptive structure?

To utilize an async validator in responsive structures, start by infusing the validator into the constructor of the part class. Then, pass the validator capability straightforwardly to FormControl to apply it.

How would you approve a timestamp?

Suppose you wish to approve the timestamp put away in a WS-Security Username Token or SAML statement. In that case, you can utilize the WS-Security Username Token Authentication, SAML Authentication, SAML Authorization, or SAML Attribute channels to play out this approval.

Conclusion

Through this article we have gained an insight on int validator in struts. We also covered the boundaries, functionalities and implementation of this validator with an hands on example. With that we will conclude our article.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in pygameCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass