validatePhoneWithCountryCode method
Validates whether the string is a valid phone number with an optional country code.
This method allows 10 to 15 digits, optionally prefixed with a country code
(+
followed by 1-3 digits). It ensures that the main number consists only of digits.
Returns:
true
if the phone number is properly formatted with or without a country code.false
otherwise.
Example Usage:
String phone1 = "+919876543210";
bool isValid1 = phone1.validatePhoneWithCountryCode(); // true
String phone2 = "0987654321";
bool isValid2 = phone2.validatePhoneWithCountryCode(); // true
String phone3 = "+1-800-123-4567";
bool isValid3 = phone3.validatePhoneWithCountryCode(); // false (invalid characters)
Implementation
bool validatePhoneWithCountryCode() =>
hasMatch(this, r'^(\+?\d{1,3})?[\d]{10,15}$');