phoneNumberValidator static method

String? phoneNumberValidator(
  1. String? value, [
  2. String? errorMessage
])

phoneNumberValidator to validate a phone number Accepts phone numbers with or without country code, and without any special characters

validator: (value) => SimpleValidations.phoneNumberValidator(value, [errorMessage]),

Implementation

static String? phoneNumberValidator(String? value, [String? errorMessage]) {
  RegExp regex = CustomRegEx.phoneRegex;
  if (value == null || value.isEmpty) {
    return errorMessage ?? 'Required';
  } else if (!regex.hasMatch(value)) {
    return errorMessage ?? 'Please enter a valid phone number';
  }
  return null;
}