Class EmailValidator<T>
- Type Parameters:
T- The type of value being validated (typically String)
- All Implemented Interfaces:
Validator<T>
EmailValidator checks that a value matches a standard email address format.
Null and empty values pass validation - use RequiredValidator to enforce non-null.
The validation uses a reasonably strict pattern that accepts most valid email addresses while rejecting obviously invalid ones. For mission-critical email validation, consider following up with an email verification service.
Usage:
// Basic email validation
EmailValidator<String> validator = new EmailValidator<>();
validator.validate("user@example.com", context); // Valid
validator.validate("invalid-email", context); // Invalid
validator.validate("", context); // Valid (empty)
validator.validate(null, context); // Valid (null)
// With custom message
EmailValidator<String> custom = new EmailValidator<String>()
.withMessage("Please enter a valid email address");
// Fluent in ValidatedInput
new ValidatedInput<>(input, errorSpan)
.required()
.email();
- Since:
- 2025
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates an EmailValidator with the default message.EmailValidator(String message) Creates an EmailValidator with a custom message. -
Method Summary
Modifier and TypeMethodDescriptionReturns the default error message for this validator.validate(T value, ValidationContext context) Validates the given value.withMessage(String message) Creates a new validator with a custom error message.
-
Constructor Details
-
EmailValidator
public EmailValidator()Creates an EmailValidator with the default message. -
EmailValidator
Creates an EmailValidator with a custom message.- Parameters:
message- The error message
-
-
Method Details
-
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
-