🙋Introduction
Sometimes you would want input to be in some specific range. For the same reason, we validate an input by checking if it follows certain specifications.
For this purpose, we have many built-in validators called Bundled Validators that Struts2 Validation Framework provides. Some of the built-in validators in the framework are email validator, date validator, int validator, and some more. Let us now see how an int validator works in Struts2.
Int Validator✅
The int validator in the bundled validator checks for an integer if it is in a given range. The parameters defined for the int validator are :
Parameters of Int Validator🤔
S.No. |
PARAMETER |
PURPOSE |
1. |
fieldName | It holds the field value the validator has to validate. It is required when we use Plain Validator Syntax. |
2. |
min | It holds the minimum value for the field the validator is validating. It is not mandatory to have this parameter set. |
3. |
max | It holds the maximum value for the field the validator is validating. It is not mandatory to have this parameter set. |
Because an int validator is a bundled validator, there are two ways we can use it:
Ways to use Int Validator ✍️
1. Plain Validator Syntax: For Action level validation, we use plain validator syntax. We can apply a single validator to multiple fields. For example:
<validators>
<!-- Plain Validator Syntax for int validation -->
<validator type="int">
<param name="fieldName"> Field Name </param>
<param name="min"> minimum integer value </param>
<param name="max"> maximum integer value </param>
<message> Message to be displayed! </message>
</validator>
</validators>
2. Field Validator Syntax: For Field level validation, we use field validator syntax. We can apply multiple validators to a single field. For example:
<validators>
<!-- Field Validator Syntax for int validation -->
<field name="Field Name">
<field-validator type="int">
<param name="min"> minimum integer value </param>
<param name="max"> maximum integer value </param>
<message> Message to be displayed! </message>
</field-validator>
</field>
</validators>
An example will clarify how we can use an int validator in our programs.