@DoubleRangeFieldValidator and @IntRangeFieldValidator in Struts 2
June 15, 2014
@DoubleRangeFieldValidator and @IntRangeFieldValidator are the strust 2 annotation to validate form field. Both validates the form field value for a given min and max. @DoubleRangeFieldValidator checks for double data type and @IntRangeFieldValidator checks for integer value.
@DoubleRangeFieldValidator Annotation in Struts 2
@DoubleRangeFieldValidator checks for double data type value entered by the user. User can entered data in a specified range as min and max. If we do not set min and max, nothing will be validated. Use @DoubleRangeFieldValidator at method level.message : This attribute is required. This displays the error message.
key : This is I18 key which is obtained from property file.
type : The value of type can be like ValidatorType.FIELD enum.
minInclusive : Minimum double data type value .
maxInclusive : Maximum double data type value .
Syntax
@DoubleRangeFieldValidator(message = "message", key = "i18n.key", shortCircuit = true, minInclusive = "0.54", maxInclusive = "66.98")
public class UserAction extends ActionSupport{ private double price; public String execute() { return SUCCESS; } @DoubleRangeFieldValidator(message = "Enter valid price", minInclusive = "0.54", maxInclusive = "66.98") public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
@IntRangeFieldValidator Annotation in Struts 2
@IntRangeFieldValidator validates a field for the integer number for the given range. We need to set min and max integer , otherwise nothing will be validated. Use @IntRangeFieldValidator at method level.message : This attribute is required. This displays the error message.
key : This is I18 key which is obtained from property file.
type : The value of type can be like ValidatorType.FIELD enum.
min : Minimum value of integer entered by user.
max: Maximum value of integer entered by user.
Syntax
@IntRangeFieldValidator(message = " message", key = "i18n.key", shortCircuit = true, min = "50", max = "100")
public class UserAction extends ActionSupport{ private int count; public String execute() { return SUCCESS; } @IntRangeFieldValidator(message = "Enter Valid Count.", min = "50", max = "100") public int getCount() { return count; } public void setCount(int count) { this.count = count; } }