Class PhoneValidator<T>

java.lang.Object
com.oorian.validation.validators.PhoneValidator<T>
Type Parameters:
T - The type of value being validated (typically String)
All Implemented Interfaces:
Validator<T>

public class PhoneValidator<T> extends Object implements Validator<T>
Validates phone number formats.

PhoneValidator supports multiple phone number formats including US, international, and E.164. Null and empty values pass validation - use RequiredValidator to enforce non-null.

Supported Formats:

  • US: (xxx) xxx-xxxx, xxx-xxx-xxxx, xxx.xxx.xxxx, xxxxxxxxxx
  • INTERNATIONAL: +x xxx xxx xxxx (variable length with country code)
  • E164: +xxxxxxxxxxx (up to 15 digits, international standard)
  • ANY: Accepts all above formats

Usage:


 // US format (default)
 PhoneValidator<String> usPhone = new PhoneValidator<>();
 usPhone.validate("(555) 123-4567", context);  // Valid
 usPhone.validate("555-123-4567", context);    // Valid
 usPhone.validate("+1 555 123 4567", context); // Invalid (use INTERNATIONAL)

 // International format
 PhoneValidator<String> intlPhone = new PhoneValidator<>(Format.INTERNATIONAL);
 intlPhone.validate("+1 555 123 4567", context);  // Valid
 intlPhone.validate("+44 20 7946 0958", context); // Valid

 // E.164 format
 PhoneValidator<String> e164 = new PhoneValidator<>(Format.E164);
 e164.validate("+15551234567", context);  // Valid

 // Any format
 PhoneValidator<String> anyPhone = new PhoneValidator<>(Format.ANY);

 // With extensions
 PhoneValidator<String> withExt = new PhoneValidator<String>()
     .allowExtensions()
     .withMessage("Please enter a valid phone number");

 // Fluent in ValidatedInput
 new ValidatedInput<>(input, errorSpan)
     .required()
     .phone(PhoneValidator.Format.US);
 
Since:
2025
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • PhoneValidator

      public PhoneValidator()
      Creates a PhoneValidator with US format (default).
    • PhoneValidator

      public PhoneValidator(PhoneValidator.Format format)
      Creates a PhoneValidator with the specified format.
      Parameters:
      format - The phone format to validate against
    • PhoneValidator

      public PhoneValidator(PhoneValidator.Format format, String message)
      Creates a PhoneValidator with a custom message.
      Parameters:
      format - The phone format to validate against
      message - The error message
  • Method Details

    • setFormat

      public PhoneValidator<T> setFormat(PhoneValidator.Format format)
      Sets the phone format to validate against.
      Parameters:
      format - The phone format
      Returns:
      This validator for method chaining
    • allowExtensions

      public PhoneValidator<T> allowExtensions()
      Allows phone number extensions (ext., x, extension).
      Returns:
      This validator for method chaining
    • validate

      public ValidationResult validate(T value, ValidationContext context)
      Description copied from interface: Validator
      Validates the given value.

      Implementations should return ValidationResult.valid() when the value passes validation, or ValidationResult.invalid(String) with an appropriate message when it fails.

      Validators typically should not fail on null values - use RequiredValidator to enforce non-null requirements. This allows validators to be composed without unexpected failures.

      Specified by:
      validate in interface Validator<T>
      Parameters:
      value - The value to validate (may be null)
      context - The validation context for accessing other field values
      Returns:
      The validation result
    • getMessage

      public String getMessage()
      Description copied from interface: Validator
      Returns the default error message for this validator.
      Specified by:
      getMessage in interface Validator<T>
      Returns:
      The error message
    • withMessage

      public PhoneValidator<T> withMessage(String message)
      Description copied from interface: Validator
      Creates a new validator with a custom error message.

      This method returns a new instance with the custom message, leaving the original validator unchanged.

      Specified by:
      withMessage in interface Validator<T>
      Parameters:
      message - The custom error message
      Returns:
      A new validator instance with the custom message
    • getFormat

      public PhoneValidator.Format getFormat()
      Returns the phone format being validated.
      Returns:
      The phone format
    • isExtensionsAllowed

      public boolean isExtensionsAllowed()
      Returns whether extensions are allowed.
      Returns:
      true if extensions are allowed