isPassword method

bool isPassword (
  1. String password,
  2. {int minLength = 4,
  3. int maxLength,
  4. bool shouldContainNumber = false,
  5. bool shouldContainSpecialChars = false,
  6. bool shouldContainCapitalLetter = false,
  7. Function reason,
  8. void isNumberPresent(
    1. bool
    ),
  9. void isSpecialCharsPresent(
    1. bool
    ),
  10. void isCapitalLetterPresent(
    1. bool
    ),
  11. void isMaxLengthFailed(
      ),
    1. void isMinLengthFailed(
        )}
      )

      Todo: Implement reason for failure

      Implementation

      static bool isPassword(
        String password, {
        int minLength = 4,
        int maxLength,
        bool shouldContainNumber = false,
        bool shouldContainSpecialChars = false,
        bool shouldContainCapitalLetter = false,
        Function reason,
        void Function(bool) isNumberPresent,
        void Function(bool) isSpecialCharsPresent,
        void Function(bool) isCapitalLetterPresent,
        void Function() isMaxLengthFailed,
        void Function() isMinLengthFailed,
      }) {
        if (password == null) {
          return false;
        }
        if (password.trim().length == 0) {
          return false;
        }
      
        if (password.length < minLength) {
          if (isMinLengthFailed != null) isMinLengthFailed();
          return false;
        }
      
        if (maxLength != null) {
          if (password.length > maxLength) {
            if (isMaxLengthFailed != null) isMaxLengthFailed();
            return false;
          }
        }
      
        if (shouldContainNumber) {
          final numberRegex = RegExp(r"[0-9]+");
          if (!numberRegex.hasMatch(password)) {
            if (isNumberPresent != null) isNumberPresent(false);
            return false;
          } else if (isNumberPresent != null) isNumberPresent(true);
        }
      
        if (shouldContainCapitalLetter) {
          final capitalRegex = RegExp(r"[A-Z]+");
          if (!capitalRegex.hasMatch(password)) {
            if (isCapitalLetterPresent != null) isCapitalLetterPresent(false);
            return false;
          } else if (isCapitalLetterPresent != null) isCapitalLetterPresent(true);
        }
      
        if (shouldContainSpecialChars) {
      //      final numberRegex = RegExp(r'(?=.*?[#?!@$%^&*-])');
          final specialRegex = RegExp(r"[\'^£$%!&*()}{@#~?><>,.|=_+¬-]");
          if (!specialRegex.hasMatch(password)) {
            if (isSpecialCharsPresent != null) isSpecialCharsPresent(false);
            return false;
          } else if (isSpecialCharsPresent != null) isSpecialCharsPresent(true);
        }
      
        return true;
      }