How to handle TextField validation in password in Flutter

前端 未结 5 1029
悲哀的现实
悲哀的现实 2020-12-29 08:34

I created a login page and I need to add these things to my password. How do I do it with validation alert message?

  • Minimum 1 Upper case
5条回答
  •  温柔的废话
    2020-12-29 09:14

    Your regular expression should look like:

    r'^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#\$&*~]).{8,}$
    

    Here is an explanation:

    r'^
      (?=.*[A-Z])       // should contain at least one upper case
      (?=.*[a-z])       // should contain at least one lower case
      (?=.*?[0-9])          // should contain at least one digit
     (?=.*?[!@#\$&*~]).{8,}  // should contain at least one Special character
    $
    

    Match above expression with your password string.Using this method-

    String validatePassword(String value) {
        Pattern pattern =
            r'^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#\$&*~]).{8,}$';
        RegExp regex = new RegExp(pattern);
        print(value);
        if (value.isEmpty) {
          return 'Please enter password';
        } else {
          if (!regex.hasMatch(value))
            return 'Enter valid password';
          else
            return null;
        }
      }
    

提交回复
热议问题