userPinValidator function
This function performs validation on a user-entered PIN value. It ensures that
the PIN is not empty, consists of only digits, and has a minimum length of four.
The function returns an error message string if the validation fails, otherwise
it returns null
to indicate a valid PIN.
@param val The user-entered PIN value to be validated.
@return An error message string if validation fails, otherwise null
.
Implementation
String? userPinValidator(dynamic val) {
final String value = val as String;
if (value.isEmpty) {
return 'A PIN is required';
}
if (!RegExp(r'^-?[0-9]+$').hasMatch(value)) {
return 'Only digits are allowed, 0-9';
}
if (value.length < 4) {
return 'Enter a four digit PIN';
}
return null;
}