Package com.oorian.validation.validators
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>
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 Summary
ConstructorsConstructorDescriptionCreates an AlphanumericValidator that only allows letters and digits.AlphanumericValidator(String message) Creates an AlphanumericValidator with a custom message. -
Method Summary
Modifier and TypeMethodDescriptionallowCharacters(String characters) Allows specific additional characters.Allows spaces in the value.Returns the additional allowed characters.Returns the default error message for this validator.booleanReturns whether spaces are allowed.validate(T value, ValidationContext context) Validates the given value.withMessage(String message) Creates a new validator with a custom error message.
-
Constructor Details
-
AlphanumericValidator
public AlphanumericValidator()Creates an AlphanumericValidator that only allows letters and digits. -
AlphanumericValidator
Creates an AlphanumericValidator with a custom message.- Parameters:
message- The error message
-
-
Method Details
-
allowSpaces
Allows spaces in the value.- Returns:
- This validator for method chaining
-
allowCharacters
Allows specific additional characters.- Parameters:
characters- The characters to allow (e.g., "-_.")- Returns:
- This validator for method chaining
-
validate
Description copied from interface:ValidatorValidates the given value.Implementations should return
ValidationResult.valid()when the value passes validation, orValidationResult.invalid(String)with an appropriate message when it fails.Validators typically should not fail on null values - use
RequiredValidatorto enforce non-null requirements. This allows validators to be composed without unexpected failures. -
getMessage
Description copied from interface:ValidatorReturns the default error message for this validator.- Specified by:
getMessagein interfaceValidator<T>- Returns:
- The error message
-
withMessage
Description copied from interface:ValidatorCreates 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:
withMessagein interfaceValidator<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
Returns the additional allowed characters.- Returns:
- The allowed characters
-