Package com.oorian.validation.validators
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>
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:
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionCreates a PhoneValidator with US format (default).PhoneValidator(PhoneValidator.Format format) Creates a PhoneValidator with the specified format.PhoneValidator(PhoneValidator.Format format, String message) Creates a PhoneValidator with a custom message. -
Method Summary
Modifier and TypeMethodDescriptionAllows phone number extensions (ext., x, extension).Returns the phone format being validated.Returns the default error message for this validator.booleanReturns whether extensions are allowed.setFormat(PhoneValidator.Format format) Sets the phone format to validate against.validate(T value, ValidationContext context) Validates the given value.withMessage(String message) Creates a new validator with a custom error message.
-
Constructor Details
-
PhoneValidator
public PhoneValidator()Creates a PhoneValidator with US format (default). -
PhoneValidator
Creates a PhoneValidator with the specified format.- Parameters:
format- The phone format to validate against
-
PhoneValidator
Creates a PhoneValidator with a custom message.- Parameters:
format- The phone format to validate againstmessage- The error message
-
-
Method Details
-
setFormat
Sets the phone format to validate against.- Parameters:
format- The phone format- Returns:
- This validator for method chaining
-
allowExtensions
Allows phone number extensions (ext., x, extension).- 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
-
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
-