isPassword static 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. bool shouldContainSmallLetter = false,
  8. Function? reason,
  9. void isNumberPresent(
    1. bool
    )?,
  10. void isSpecialCharsPresent(
    1. bool
    )?,
  11. void isCapitalLetterPresent(
    1. bool
    )?,
  12. void isSmallLetterPresent(
    1. bool
    )?,
  13. void isMaxLengthFailed(
      )?,
    1. void isMinLengthFailed(
        )?}
      )

      Todo: Implement reason for failure For Validated passwords strings

      Implementation

      static bool isPassword(
        String? password, {
        int minLength = 4,
        int? maxLength,
        bool shouldContainNumber = false,
        bool shouldContainSpecialChars = false,
        bool shouldContainCapitalLetter = false,
        bool shouldContainSmallLetter = false,
        Function? reason,
        void Function(bool)? isNumberPresent,
        void Function(bool)? isSpecialCharsPresent,
        void Function(bool)? isCapitalLetterPresent,
        void Function(bool)? isSmallLetterPresent,
        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 (shouldContainSmallLetter) {
          final smallLetterRegex = RegExp(r"[a-z]+");
          if (!smallLetterRegex.hasMatch(password)) {
            if (isSmallLetterPresent != null) isSmallLetterPresent(false);
            return false;
          } else if (isSmallLetterPresent != null) isSmallLetterPresent(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;
      }