【问题标题】:How can I make a strong password in flutter? [duplicate]如何在颤振中制作强密码? [复制]
【发布时间】:2022-01-20 11:43:47
【问题描述】:

我正在我的应用程序中制作登录表单。 这是我的密码强度标准如下:

  • 8 个字符长度
  • 2 个大写字母
  • 1 个特殊字符 (!@#$&*)
  • 2 个数字 (0-9)
  • 三个小写字母

【问题讨论】:

  • 对吗?那么问题是什么?

标签: flutter dart authentication


【解决方案1】:

您需要使用正则表达式来验证结构。

bool validateStructure(String value){
        String  pattern = r'^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#\$&*~]).{8,}$';
        RegExp regExp = new RegExp(pattern);
        return regExp.hasMatch(value);
  }


  var _usernameController = TextEditingController();
    String _usernameError;

    ...

    @override
    Widget build(BuildContext context) {
        return
        ...
        TextFormField(
          controller: _usernameController,
          decoration: InputDecoration(
              hintText: "Username", errorText: _usernameError),
          style: TextStyle(fontSize: 18.0),
        ),
        Container(
          width: double.infinity,
          height: 50.0,
          child: RaisedButton(
            onPressed: validate,
            child: Text(
              "Login",
              style: TextStyle(color: Colors.white),
            ),
            color: Theme.of(context).primaryColor,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(50.0),
            ),
          ),
        ),
        ...
    }

    ...

    validate(){
        if(!validateStructure(_usernameController.text)){
            setState(() {
                _usernameError = emailError;
                _passwordError = passwordError;
            });
            // show dialog/snackbar to get user attention.
            return;
        }
        // Continue 
    }

【讨论】:

  • 谢谢你救我!
  • 欢迎@JohnBenedictSabando。您能否让我的答案正确并投票,以便其他有同样问题的人有所帮助。
【解决方案2】:
you can form_field_validator plugin and you can change it accordingly to your requirements. Hope this will work for you, Thanks
        
         link of the plugin. https://pub.dev/packages/form_field_validator 
        
            import 'package:form_field_validator/form_field_validator.dart';
            
            TextFormField(                               
           controller: passwordController,
           validator: MultiValidator([
           RequiredValidator(errorText: "* Required"),
           MinLengthValidator(6,
           errorText:
           "Password should be atleast 6 characters"),
           PatternValidator(r'(?=.*?[#?!@$%^&*-])',
           errorText:
          'passwords must have at least one special character'),
           MaxLengthValidator(15,
           errorText:
           "Password should not be greater than 15 characters")
            ]),
          obscureText: Provider.of<LoginProvider>(context,
           listen: false)
           .isTrue,
           keyboardType: TextInputType.emailAddress,
           autofillHints: [AutofillHints.email],
           cursorColor: AppColors.black,
           style: TextStyle(
           color: Color(0xff919AAA),),
           decoration: InputDecoration(
           border: OutlineInputBorder(
           borderRadius: BorderRadius.circular(25.0),
           borderSide: BorderSide(
           color: AppColors.textFieldColor,
                                            ),
                                          ),
                                          focusedBorder: OutlineInputBorder(
                                            borderRadius: BorderRadius.circular(25.0),
                                            borderSide: BorderSide(
                                              color: AppColors.textFieldColor,
                                            ),
                                          ),
                                          errorBorder: OutlineInputBorder(
                                            borderRadius: BorderRadius.circular(25.0),
                                            borderSide: BorderSide(
                                              color: AppColors.red,
                                            ),
                                          ),
                                          focusedErrorBorder: OutlineInputBorder(
                                            borderRadius: BorderRadius.circular(25.0),
                                            borderSide: BorderSide(
                                              color: AppColors.red,
                                            ),
                                          ),
                                          enabledBorder: OutlineInputBorder(
                                            borderRadius: BorderRadius.circular(25.0),
                                            borderSide: BorderSide(
                                              color: AppColors.textFieldColor,
                                            ),
                                          ),
                                          floatingLabelBehavior:
                                              FloatingLabelBehavior.never,
                                          suffixIcon: IconButton(
                                            onPressed: () {
                                              Provider.of<LoginProvider>(context,
                                                      listen: false)
                                                  .toggleObs();
                                            },
                                            icon: Provider.of<LoginProvider>(context,
                                                    listen: false)
                                                .switchObsIcon,
                                          ),
                                          labelText: 'Password',
                                          errorStyle: TextStyle(color: AppColors.red),
                                          hintStyle: TextStyle(
                                            color: Color(0xff919AAA),
                                          ),
                                        ),
                                      ),

【讨论】:

  • 非常感谢!
猜你喜欢
  • 2021-11-25
  • 2020-10-13
  • 2022-01-14
  • 2019-01-30
  • 1970-01-01
  • 1970-01-01
  • 2020-04-27
  • 2019-12-12
  • 1970-01-01
相关资源
最近更新 更多