PatternValidator class

A validator that checks whether a string matches a specific regular expression pattern.

The PatternValidator ensures that the input string adheres to a given regex pattern. This allows for flexible validation of various formats, such as custom identifiers, alphanumeric constraints, or other structured inputs.

Example

final validator = PatternValidator(
  r'^[A-Za-z0-9]+$',
  message: 'Only letters and numbers are allowed',
);

print(validator.validate('Valid123')); // null (valid)
print(validator.validate('Invalid@!')); // 'Only letters and numbers are allowed' (invalid)

Parameters

  • pattern: The regular expression pattern used for validation.
  • message: The error message returned if the validation fails.

Behavior:

  • If the input value is null, validation fails.
  • If the input does not match the specified regex pattern, validation fails.
  • If the input matches the pattern, validation passes (null is returned).
Inheritance
Implementers

Constructors

PatternValidator.new(String pattern, {required String message})
Creates a PatternValidator with the given pattern and message.

Properties

hashCode int
The hash code for this object.
no setterinherited
message String
The error message to be returned when validation fails.
finalinherited
pattern String
The regex pattern used for validation.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited
validate(covariant String value) String?
Validates the provided value.
override

Operators

operator ==(Object other) bool
The equality operator.
inherited