NumberValidator class
A validator that checks if a control's value represents a valid number.
This validator can be used for both String and num types. It provides
options to allow or disallow null values, negative numbers, and to specify
the number of allowed decimal places.
Example:
final control = FormControl<String>(
validators: [Validators.number()],
);
control.value = '123';
print(control.valid); // true
control.value = 'abc';
print(control.valid); // false
control.value = '12.34';
print(control.valid); // false, because decimals are not allowed by default
To allow decimals, you can use the allowedDecimals parameter:
final decimalControl = FormControl<String>(
validators: [Validators.number(allowedDecimals: 2)],
);
decimalControl.value = '12.34';
print(decimalControl.valid); // true
decimalControl.value = '12.345';
print(decimalControl.valid); // false, as it exceeds the allowed decimal places
Constructors
- NumberValidator({bool allowNull = false, int allowedDecimals = 0, bool allowNegatives = true})
-
Creates a new NumberValidator instance to validate strings representing numbers.
const
Properties
- allowedDecimals → int
-
The allowed number of decimal places in the validated string.
final
- allowNegatives → bool
-
Whether the validator allows negative numbers.
final
- allowNull → bool
-
Whether the validator allows null values.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
call(
AbstractControl control) → Map< String, dynamic> ? -
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
validate(
AbstractControl control) → Map< String, dynamic> ? -
Validates the
control.override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- notNumbersRegex → RegExp
-
The regex expression of a numeric string value.
final