This package contains independant validation routines adapted from Apache Commons Validator 1.4.0.

Table of Contents

1. Overview

Commons Validator serves two purposes:

This package has been created, since version 1.3.0, in an attempt to clearly separate these two concerns and is the location for the standard, independant validation routines/functions in Commons Validator.

The contents of this package have no dependencies on the framework aspect of Commons Validator and can be used on their own.

2. Validators

2.1 Overview

This section lists other available validators.

2.2 Regular Expression Validation

Regular expression validation can be done either by using the static methods provied by RegexValidator or by creating a new instance, which caches and re-uses compiled Patterns.

Below is an example of using one of the static methods to validate, matching in a case insensitive manner and returning a String of the matched groups (which doesn't include the hyphen).

      // set up the parameters
      boolean caseSensitive   = false;
      String regex            = "^([A-Z]*)(?:\\-)([A-Z]*)$";

      // validate - result should be a String of value "abcdef"
      String result = RegexValidator.validate("abc-def", regex, caseSensitive);

The following static methods are provided for regular expression validation:

Below is an example of creating an instance of RegexValidator matching in a case insensitive manner against a set of regular expressions:

      // set up the parameters
      boolean caseSensitive = false;
      String regex1   = "^([A-Z]*)(?:\\-)([A-Z]*)*$"
      String regex2   = "^([A-Z]*)$";
      String[] regexs = new String[] {regex1, regex1};

      // Create the validator
      RegexValidator validator = new RegexValidator(regexs, caseSensitive);

      // Validate true/false
      boolean valid = validator.isValid("abc-def");

      // Validate and return a String
      String result = validator.validate("abc-def");

      // Validate and return a String[]
      String[] groups = validator.match("abc-def");

See the RegexValidator javadoc for a full list of the available constructors.

2.3 IP Address Validation

InetAddressValidator provides IPv4 address validation.

For example:

      // Get an InetAddressValidator
      InetAddressValidator validator = InetAddressValidator.getInstance();

      // Validate an IPv4 address
      if (!validator.isValid(candidateInetAddress)) {
          ... // invalid

2.4 Email Address Validation

EmailValidator provides email address validation according to RFC 822 standards.

For example:

      // Get an EmailValidator
      EmailValidator validator = EmailValidator.getInstance();

      // Validate an email address
      boolean isAddressValid = validator.isValid("");

      // Validate a variable containing an email address
      if (!validator.isValid(addressFromUserForm)) {
          webController.sendRedirect(ERROR_REDIRECT, "Email address isn't valid");
          // etc.

2.5 URL Validation

UrlValidator provides URL validation by checking the scheme, authority, path, query, and fragment in turn. Clients may specify valid schemes to be used in validating in addition to or instead of the default values (HTTP, HTTPS, FTP). The UrlValidator also supports options that change the parsing rules; for example, the ALLOW_2_SLASHES option instructs the Validator to allow consecutive slash characters in the path component, which is considered an error by default. For more information on the available options, see the UrlValidator documentation.

For example:

      // Get an UrlValidator
      UrlValidator defaultValidator = new UrlValidator(); // default schemes
      if (defaultValidator.isValid("")) {
          ... // valid
      if (!defaultValidator.isValid("http//")) {
          ... // invalid

      // Get an UrlValidator with custom schemes
      String[] customSchemes = { "sftp", "scp", "https" };
      UrlValidator customValidator = new UrlValidator(customSchemes);
      if (!customValidator.isValid("")) {
          ... // invalid due to insecure protocol

      // Get an UrlValidator that allows double slashes in the path
      UrlValidator doubleSlashValidator = new UrlValidator(UrlValidator.ALLOW_2_SLASHES);
      if (doubleSlashValidator.isValid("")) {
          ... // valid only in this Validator instance

2.6 Domain Name Validation

DomainValidator provides validation of Internet domain names as specified by RFC1034/RFC1123 and according to the IANA-recognized list of top-level domains (TLDs). Clients may validate an entire domain name, a TLD of any category, or a TLD within a specific category.

For example:

      // Get a DomainValidator
      DomainValidator validator = DomainValidator.getInstance();

      // Validate a domain name
      if (validator.isValid("")) {
          ... // valid
      if (!validator.isValid("www.apache.wrong")) {
          ... // invalid

      // Validate a TLD
      if (validator.isValidTld(".com")) {
          ... // valid
      if (validator.isValidTld("org")) {
          ... // valid, the leading dot is optional
      if (validator.isValidTld(".us")) {
          ... // valid, country code TLDs are also accepted

      // Validate TLDs in categories
      if (validator.isValidGenericTld(".name")) {
          ... // valid
      if (!validator.isValidGenericTld(".uk")) {
          ... // invalid, .uk is a country code TLD
      if (!validator.isValidCountryCodeTld(".info")) {
          ... // invalid, .info is a generic TLD