Class CodeValidator
- All Implemented Interfaces:
Serializable
CheckDigit
validations.
Performs the following validations on a code:
- if the code is null, return null/false as appropriate
- trim the input. If the resulting code is empty, return null/false as appropriate
- Check the format of the code using a regular expression. (if specified)
- Check the minimum and maximum length (if specified) of the parsed code (i.e. parsed by the regular expression).
- Performs
CheckDigit
validation on the parsed code (if specified). - The
validate(String)
method returns the trimmed, parsed input (or null if validation failed)
Note
The isValid(String)
method will return true if the input passes validation.
Since this includes trimming as well as potentially dropping parts of the input,
it is possible for a String to pass validation
but fail the checkdigit test if passed directly to it (the check digit routines generally don't trim input
nor do they generally check the format/length).
To be sure that you are passing valid input to a method use validate(String)
as follows:
Object valid = validator.validate(input); if (valid != null) { some_method(valid.toString()); }
Configure the validator with the appropriate regular expression, minimum/maximum length
and CheckDigit
validator and then call one of the two validation
methods provided:
boolean isValid(code)
String validate(code)
Codes often include format characters - such as hyphens - to make them
more easily human readable. These can be removed prior to length and check digit
validation by specifying them as a non-capturing group in the regular
expression (i.e. use the (?: )
notation).
Or just avoid using parentheses except for the parts you want to capture
- Since:
- Validator 1.4
- Version:
- $Revision: 1713388 $
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final CheckDigit
private final int
private final int
private final RegexValidator
private static final long
-
Constructor Summary
ConstructorsConstructorDescriptionCodeValidator
(String regex, int minLength, int maxLength, CheckDigit checkdigit) Construct a code validator with a specified regular expression, minimum/maximum length andCheckDigit
validation.CodeValidator
(String regex, int length, CheckDigit checkdigit) Construct a code validator with a specified regular expression, length andCheckDigit
.CodeValidator
(String regex, CheckDigit checkdigit) Construct a code validator with a specified regular expression andCheckDigit
.CodeValidator
(RegexValidator regexValidator, int minLength, int maxLength, CheckDigit checkdigit) Construct a code validator with a specified regular expression validator, minimum/maximum length andCheckDigit
validation.CodeValidator
(RegexValidator regexValidator, int length, CheckDigit checkdigit) Construct a code validator with a specified regular expression, validator, length andCheckDigit
validation.CodeValidator
(RegexValidator regexValidator, CheckDigit checkdigit) Construct a code validator with a specified regular expression, validator andCheckDigit
validation. -
Method Summary
Modifier and TypeMethodDescriptionReturn the check digit validation routine.int
Return the maximum length of the code.int
Return the minimum length of the code.Return the regular expression validator.boolean
Validate the code returning eithertrue
orfalse
.Validate the code returning either the valid code ornull
if invalid.
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
regexValidator
-
minLength
private final int minLength -
maxLength
private final int maxLength -
checkdigit
-
-
Constructor Details
-
CodeValidator
Construct a code validator with a specified regular expression andCheckDigit
. The RegexValidator validator is created to be case-sensitive- Parameters:
regex
- The format regular expressioncheckdigit
- The check digit validation routine
-
CodeValidator
Construct a code validator with a specified regular expression, length andCheckDigit
. The RegexValidator validator is created to be case-sensitive- Parameters:
regex
- The format regular expression.length
- The length of the code (sets the mimimum/maximum to the same)checkdigit
- The check digit validation routine
-
CodeValidator
Construct a code validator with a specified regular expression, minimum/maximum length andCheckDigit
validation. The RegexValidator validator is created to be case-sensitive- Parameters:
regex
- The regular expressionminLength
- The minimum length of the codemaxLength
- The maximum length of the codecheckdigit
- The check digit validation routine
-
CodeValidator
Construct a code validator with a specified regular expression, validator andCheckDigit
validation.- Parameters:
regexValidator
- The format regular expression validatorcheckdigit
- The check digit validation routine.
-
CodeValidator
Construct a code validator with a specified regular expression, validator, length andCheckDigit
validation.- Parameters:
regexValidator
- The format regular expression validatorlength
- The length of the code (sets the mimimum/maximum to the same value)checkdigit
- The check digit validation routine
-
CodeValidator
public CodeValidator(RegexValidator regexValidator, int minLength, int maxLength, CheckDigit checkdigit) Construct a code validator with a specified regular expression validator, minimum/maximum length andCheckDigit
validation.- Parameters:
regexValidator
- The format regular expression validatorminLength
- The minimum length of the codemaxLength
- The maximum length of the codecheckdigit
- The check digit validation routine
-
-
Method Details
-
getCheckDigit
Return the check digit validation routine.N.B. Optional, if not set no Check Digit validation will be performed on the code.
- Returns:
- The check digit validation routine
-
getMinLength
public int getMinLength()Return the minimum length of the code.N.B. Optional, if less than zero the minimum length will not be checked.
- Returns:
- The minimum length of the code or
-1
if the code has no minimum length
-
getMaxLength
public int getMaxLength()Return the maximum length of the code.N.B. Optional, if less than zero the maximum length will not be checked.
- Returns:
- The maximum length of the code or
-1
if the code has no maximum length
-
getRegexValidator
Return the regular expression validator.N.B. Optional, if not set no regular expression validation will be performed on the code.
- Returns:
- The regular expression validator
-
isValid
Validate the code returning eithertrue
orfalse
.- Parameters:
input
- The code to validate- Returns:
true
if valid, otherwisefalse
-
validate
Validate the code returning either the valid code ornull
if invalid.- Parameters:
input
- The code to validate- Returns:
- The code if valid, otherwise
null
if invalid
-