validate method
- String? value
override
Validates the value and returns an error string if it fails,
or null if it passes.
Implementation
@override
String? validate(String? value) {
if (value == null || value.isEmpty) return null;
RegExp regex;
if (pattern != null) {
regex = pattern!;
} else if (country != null) {
regex = switch (country!) {
PhoneCountry.us =>
RegExp(r'^\+?1?\s*\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$'),
PhoneCountry.uk => RegExp(r'^\+?44[\s.-]?\d{10}$'),
PhoneCountry.india => RegExp(r'^\+?91[\s.-]?\d{10}$'),
PhoneCountry.australia => RegExp(r'^\+?61[\s.-]?\d{9}$'),
PhoneCountry.brazil => RegExp(r'^\+?55[\s.-]?\d{10,11}$'),
PhoneCountry.germany => RegExp(r'^\+?49[\s.-]?\d{10,11}$'),
PhoneCountry.france => RegExp(r'^\+?33[\s.-]?\d{9}$'),
PhoneCountry.japan => RegExp(r'^\+?81[\s.-]?\d{10}$'),
PhoneCountry.china => RegExp(r'^\+?86[\s.-]?\d{11}$'),
};
} else {
// Generic international fallback
regex = RegExp(r'^\+?[0-9\s\-().]{7,20}$');
}
if (!regex.hasMatch(value)) {
return message;
}
return null;
}