validField method

bool validField(
  1. TextInputType type,
  2. String? value
)

Implementation

bool validField(TextInputType type, String? value) {
  errorMessage = widget.errorMessage;
  bool result = true;
  switch (type.index) {
    case 0: //text
    case 1: //multiline
      break;
    case 2: //number
      String pattern = r"^[0-9]*$";
      result = RegExp(pattern).hasMatch(value!);
      break;
    case 3: //phone
      String pattern = r"^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*$";
      result = RegExp(pattern).hasMatch(value!);
      break;
    case 4: //datetime
      break;
    case 5: //email
      String pattern =
          r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?)*$";
      result = RegExp(pattern).hasMatch(value!);
      break;
    case 6: //url
      String pattern =
          r"^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$";
      result = RegExp(pattern).hasMatch(value!);
      break;
  }
  if (result && widget.sameTo != null) {
    result = widget.sameTo!.text == value;
    if (!result) {
      this.errorMessage = widget.sameToMessage;
    }
  } else if (result && widget.minLength != null) {
    result = value!.length >= widget.minLength!;
    if (!result) {
      this.errorMessage = widget.minLengthMessage;
    }
  } else if (result && widget.maxLength != null) {
    result = value!.length <= widget.maxLength!;
    if (!result) {
      this.errorMessage = widget.maxLengthMessage;
    }
  }
  return result;
}