Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.apache.commons.validator.routines
Class CodeValidator

    java.lang.Object
      org.apache.commons.validator.routines.CodeValidator

All Implemented Interfaces:
  Serializable

    public final class CodeValidator

extends Object
implements Serializable

Generic Code Validation providing format, minimum/maximum length and `CheckDigit` validations.

Performs the following validations on a code:

- 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).

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).

Since:
  Validator 1.4
Version:
  $Revision: 1227719 $ $Date: 2012-01-05 18:45:51 +0100 (Thu, 05 Jan 2012) $
See Also:
  Serialized Form

Constructor Summary
`CodeValidator(RegexValidator regexValidator, CheckDigit checkdigit)` Construct a code validator with a specified regular expression, validator and `CheckDigit` validation.
`CodeValidator(RegexValidator regexValidator, int length, CheckDigit checkdigit)` Construct a code validator with a specified regular expression, validator, length and `CheckDigit` validation.
`CodeValidator(RegexValidator regexValidator, int minLength, int maxLength, CheckDigit checkdigit)` Construct a code validator with a specified regular expression validator, minimum/maximum length and `CheckDigit` validation.
`CodeValidator(String regex, CheckDigit checkdigit)` Construct a code validator with a specified regular expression and `CheckDigit`.
`CodeValidator(String regex, int length, CheckDigit checkdigit)` Construct a code validator with a specified regular expression, length and `CheckDigit`.
`CodeValidator(String regex, int minLength, int maxLength, CheckDigit checkdigit)` Construct a code validator with a specified regular expression, minimum/maximum length and `CheckDigit` validation.

Method Summary
`CheckDigit` | `getCheckDigit()` Return the check digit validation routine.
`int` | `getMaxLength()` Return the maximum length of the code.
`int` | `getMinLength()` Return the minimum length of the code.
`RegexValidator` | `getRegexValidator()` Return the regular expression validator.
`boolean` | `isValid(String input)` Validate the code returning either `true` or `false`.
`Object` | `validate(String input)` Validate the code returning either the valid code or `null` if invalid.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### CodeValidator

    public CodeValidator(String regex,
                         CheckDigit checkdigit)

  Construct a code validator with a specified regular expression and `CheckDigit`.

  Parameters:
    `regex` - The format regular expression
    `checkdigit` - The check digit validation routine

### CodeValidator

    public CodeValidator(String regex,
                         int length,
                         CheckDigit checkdigit)

  Construct a code validator with a specified regular expression, length and `CheckDigit`.

  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

    public CodeValidator(String regex,
                         int minLength,
                         int maxLength,
                         CheckDigit checkdigit)

  Construct a code validator with a specified regular expression, minimum/maximum length and `CheckDigit` validation.

  Parameters:
    `regex` - The regular expression validator
    `minLength` - The minimum length of the code
    `maxLength` - The maximum length of the code
    `checkdigit` - The check digit validation routine

### CodeValidator

    public CodeValidator(RegexValidator regexValidator,
                         CheckDigit checkdigit)

  Construct a code validator with a specified regular expression, validator and `CheckDigit` validation.

  Parameters:
    `regexValidator` - The format regular expression validator
    `checkdigit` - The check digit validation routine.

### CodeValidator

    public CodeValidator(RegexValidator regexValidator,
                         int length,
                         CheckDigit checkdigit)

  Construct a code validator with a specified regular expression, validator, length and `CheckDigit` validation.

  Parameters:
    `regexValidator` - The format regular expression validator
    `length` - 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 and `CheckDigit` validation.

  Parameters:
    `regexValidator` - The format regular expression validator
    `minLength` - The minimum length of the code
    `maxLength` - The maximum length of the code
    `checkdigit` - The check digit validation routine

Method Detail

### getCheckDigit

    public CheckDigit 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

    public RegexValidator 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

    public boolean isValid(String input)

  Validate the code returning either `true` or `false`.

  Parameters:
    `input` - The code to validate
  Returns:
    `true` if valid, otherwise `false`

### validate

    public Object validate(String input)

  Validate the code returning either the valid code or `null` if invalid.

  Parameters:
    `input` - The code to validate
  Returns:
    The code if valid, otherwise `null` if invalid

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2002-2012 The Apache Software Foundation. All Rights Reserved.