Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Double Validator
3.
Parameters of double Validator
4.
Example of Double Validator
5.
Complete Example of Double Validator
5.1.
Create index.jsp for Input
5.2.
Create the Action Class.
5.3.
Create the Validation File
5.4.
Create struts.xml
5.5.
Create View Component
6.
Frequently Asked Questions
6.1.
What is the work of a validator?
6.2.
What is a validator class?
6.3.
What skills do validation engineers need?
6.4.
How do I become a validation analyst?
6.5.
What is the meaning of Validator?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

Double Validator

Introduction

A double validator checks a floating-point value to see if it falls inside a specific range. It may be used for things like product prices.

This Validator offers many Java text-based methods for verifying and converting a String value to a Double. Either use the default format for the default Locale or parse the NumberFormat.

shows the struts 2 double validation

Double Validator

The DoubleValidator type provides the non-integer number validator.

It is accepted if the input contains a double that falls within the acceptable range and is in the correct format.

If the input contains a double that is outside the acceptable range or is formatted improperly—for example, with too many digits after the decimal point—or is empty, it is accepted but considered invalid.

If an input is not a double, it is rejected.

Note: The information is rejected if the input is a negative double and the valid range only contains positive doubles (e.g., 0.0 to 100.0). The input is also rejected if the notation is set to DoubleValidator.standard notation. There are more digits before the decimal point than a double within the acceptable range can have. If the input is not within the valid range and the notation is DoubleValidator.scientific notation, the information is accepted but invalid. By altering the exponent, the value may still be considered accurate.

Parameters of double Validator

The double validator has five declared parameters.

Table of double validator parameters and their descriptions

Example of Double Validator

<validators>  
<!-- Plain Validator Syntax -->  
         <validator type="double">  
             <param name="fieldName">price</param>  
             <param name="minInclusive">100.0</param>  
             <param name="maxInclusive">10000.0</param>  
             <message>Price must be between ${minInclusive} and ${maxInclusive}</message>  
         </validator>  
</validators>  

 

<validators>  
   <!-- Field Validator Syntax -->  
         <field name="price">  
             <field-validator type="double">  
               <param name="minInclusive">100.0</param>  
               <param name="maxInclusive">10000.0</param>  
               <message>Price must be between ${minInclusive} and ${maxInclusive}</message>  
         </field-validator>  
         </field>  
 
</validators>  

Complete Example of Double Validator

Create index.jsp for Input

Utilizing the struts UI tags, this JSP website generates a form. It gets the user's name, password, and email address.

index.jsp

<%@ taglib uri="/struts-tags" prefix="s" %>  
	<html>  
		<head>  
			<STYLE type="text/css">  
			.errorMessage{color:red;}  
			</STYLE>  
		</head>  
	<body>  
 
		<s:form action="register">  
		<s:textfield name="id" label="Product Id"></s:textfield>  
		<s:textfield name="price" label="Product Price"></s:textfield>  
		<s:submit value="register"></s:submit>  
		</s:form>  
 
	</body>  
	</html>  

Create the Action Class.

The execute function is overridden in this action class, which derives from the ActionSupport class.

RegisterAction.java


import com.opensymphony.xwork2.ActionSupport;

public class Register extends ActionSupport {
  private int id;
  private double price;

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public double getPrice() {
    return price;
  }

  public void setPrice(double price) {
    this.price = price;
  }

  public String execute() {
    return "success";
  }

}

Create the Validation File

In this case, the validation is carried out utilizing bundled validators.

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="price">  
       <field-validator type="double">  
         <param name="minInclusive">100.0</param>  
         <param name="maxExclusive">9999.9</param>  
         
       <message>Price must be between ${minInclusive} to ${maxExclusive}</message>  
       </field-validator>  
       </field>  
       </validators>

Create struts.xml

This XML file specifies a workflow stack interceptor and a different outcome with the name input.

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>

Create View Component

It is a straightforward JSP file that shows the user's information.

welcome.jsp

<%@ taglib uri="/struts-tags" prefix="s" %>  
  
Product Id:<s:property value="id"/><br/>  
Product price:<s:property value="price"/>  

Frequently Asked Questions

What is the work of a validator?

A certified engineer who oversees, examines, calibrates, tests, and alters the machinery, tools, mechanics, and processes required to produce various goods is known as a validation engineer. To generate high-quality products, they ensure all systems are functioning properly and effectively.

What is a validator class?

Defines a helper class that, when used in conjunction with the accompanying ValidationAttribute attributes, may be used to validate objects, properties, and methods.

What skills do validation engineers need?

Outstanding interpersonal abilities. Comprehensive knowledge of industry standards and legal requirements. Thorough understanding of scientific procedures and applied mathematics. Extensive knowledge of associated equipment and the capacity to modify equipment.

How do I become a validation analyst?

It would help if you had a bachelor's degree in engineering or science to work as a validation analyst.

Additionally, you must be familiar with the concepts of quality assurance and have prior experience working in a manufacturing setting.

What is the meaning of Validator?

A validator is computer software used to verify a piece of code or document's validity or syntactical accuracy. Although it may be applied to any predefined format or language, the phrase is frequently used for validating HTML, CSS, and XML documents like RSS feed.

Conclusion

So that's the end of the article. Double Validator

After reading about the Double Validator, Are you interested in reading/exploring more themes on Validator?

Don't worry; Coding Ninjas has you covered.

However, if you want to give your work an edge over the competition, you might choose to enroll in one of our premium courses.

With our Coding Ninjas Studio Guided Path, you may learn about Data Structures & Algorithms, Competitive Programming, JavaScript, System Design, and more! If you want to put your coding skills to the test, check out the mock test series on Coding Ninjas Studio and participate in the contests! But if you've only recently started your schooling and are looking for answers to issues presented by digital titans like Amazon, Microsoft, Uber, and others. In this situation, you must consider the obstaclesinterview experiences, and interview package as part of your placement preparations. If you find our blogs valuable and fascinating, please vote them up!

Good luck with your studies!

Thank you
Live masterclass