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>

public class NumericValidator<T> extends Object implements 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 Details

    • NumericValidator

      public NumericValidator()
      Creates a NumericValidator that only allows digits.
    • NumericValidator

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

    • allowDecimal

      public NumericValidator<T> allowDecimal()
      Allows decimal points in the number.
      Returns:
      This validator for method chaining
    • allowNegative

      public NumericValidator<T> allowNegative()
      Allows negative numbers (leading minus sign).
      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 NumericValidator<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
    • 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