Class AlphanumericValidator<T>

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

public class AlphanumericValidator<T> extends Object implements Validator<T>
Validates that a value contains only alphanumeric characters.

AlphanumericValidator checks that a value consists only of letters (A-Z, a-z) and digits (0-9), with optional support for spaces and additional characters.

Null and empty values pass validation - use RequiredValidator to enforce non-null.

Usage:


 // Basic alphanumeric (letters and digits only)
 AlphanumericValidator<String> basic = new AlphanumericValidator<>();
 basic.validate("User123", context);     // Valid
 basic.validate("User 123", context);    // Invalid (has space)
 basic.validate("User-123", context);    // Invalid (has hyphen)

 // Allow spaces
 AlphanumericValidator<String> withSpaces = new AlphanumericValidator<String>()
     .allowSpaces()
     .withMessage("Letters, numbers, and spaces only");
 withSpaces.validate("John Doe 123", context);  // Valid

 // Allow specific characters
 AlphanumericValidator<String> custom = new AlphanumericValidator<String>()
     .allowCharacters("-_.")
     .withMessage("Invalid characters");
 custom.validate("user-name_123.test", context);  // Valid

 // Combined options
 AlphanumericValidator<String> flexible = new AlphanumericValidator<String>()
     .allowSpaces()
     .allowCharacters("-_")
     .withMessage("Only letters, numbers, spaces, hyphens, and underscores allowed");

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

    • AlphanumericValidator

      public AlphanumericValidator()
      Creates an AlphanumericValidator that only allows letters and digits.
    • AlphanumericValidator

      public AlphanumericValidator(String message)
      Creates an AlphanumericValidator with a custom message.
      Parameters:
      message - The error message
  • Method Details

    • allowSpaces

      public AlphanumericValidator<T> allowSpaces()
      Allows spaces in the value.
      Returns:
      This validator for method chaining
    • allowCharacters

      public AlphanumericValidator<T> allowCharacters(String characters)
      Allows specific additional characters.
      Parameters:
      characters - The characters to allow (e.g., "-_.")
      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 AlphanumericValidator<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
    • isSpacesAllowed

      public boolean isSpacesAllowed()
      Returns whether spaces are allowed.
      Returns:
      true if spaces are allowed
    • getAllowedCharacters

      public String getAllowedCharacters()
      Returns the additional allowed characters.
      Returns:
      The allowed characters