Package com.oorian.validation.validators
Class NumericValidator<T>
java.lang.Object
com.oorian.validation.validators.NumericValidator<T>
- Type Parameters:
T- The type of value being validated (typically String)
- All Implemented Interfaces:
Validator<T>
Validates that a value contains only numeric characters.
NumericValidator checks that a value consists only of digits, with optional support for decimal points and negative signs.
Null and empty values pass validation - use RequiredValidator to enforce non-null.
Usage:
// Basic integer validation (digits only)
NumericValidator<String> intOnly = new NumericValidator<>();
intOnly.validate("12345", context); // Valid
intOnly.validate("123.45", context); // Invalid (has decimal)
intOnly.validate("-123", context); // Invalid (has negative)
// Allow decimals
NumericValidator<String> withDecimal = new NumericValidator<String>()
.allowDecimal()
.withMessage("Please enter a valid number");
withDecimal.validate("123.45", context); // Valid
// Allow negative numbers
NumericValidator<String> withNegative = new NumericValidator<String>()
.allowNegative()
.withMessage("Please enter a valid number");
withNegative.validate("-123", context); // Valid
// Full number support
NumericValidator<String> fullNumber = new NumericValidator<String>()
.allowDecimal()
.allowNegative()
.withMessage("Please enter a valid number");
fullNumber.validate("-123.45", context); // Valid
// Fluent in ValidatedInput
new ValidatedInput<>(input, errorSpan)
.required()
.numeric();
- Since:
- 2025
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a NumericValidator that only allows digits.NumericValidator(String message) Creates a NumericValidator with a custom message. -
Method Summary
Modifier and TypeMethodDescriptionAllows decimal points in the number.Allows negative numbers (leading minus sign).Returns the default error message for this validator.booleanReturns whether decimal values are allowed.booleanReturns whether negative values 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
-
NumericValidator
public NumericValidator()Creates a NumericValidator that only allows digits. -
NumericValidator
Creates a NumericValidator with a custom message.- Parameters:
message- The error message
-
-
Method Details
-
allowDecimal
Allows decimal points in the number.- Returns:
- This validator for method chaining
-
allowNegative
Allows negative numbers (leading minus sign).- 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
-
isDecimalAllowed
public boolean isDecimalAllowed()Returns whether decimal values are allowed.- Returns:
- true if decimals are allowed
-
isNegativeAllowed
public boolean isNegativeAllowed()Returns whether negative values are allowed.- Returns:
- true if negatives are allowed
-