13
votes

I just learned Flutter. Here I want to ask how to match passwords and confirm passwords. Here I will give my code. I also use TextField. and I don't use a validator here to validate

                TextField(
                key: passkey,
                style: new TextStyle(color: Colors.white),
                controller: password,
                decoration: InputDecoration(
                  labelText: 'Password',
                  labelStyle: TextStyle(color: Colors.white),
                  hintStyle: TextStyle(color: Colors.white),
                  icon: const Padding(
                      padding: const EdgeInsets.only(top: 15.0),
                      child: const Icon(
                        Icons.lock_outline,
                        color: Colors.white,
                      )
                      ),
                  errorText: validate ? 'Password Can\'t Be Empty' : null,
                ),
                obscureText: _obscureText,

              ),
              TextField(
                style: new TextStyle(color: Colors.white),
                controller: confirmpassword,
                decoration: InputDecoration(
                  labelText: 'Retype Password',
                  labelStyle: TextStyle(color: Colors.white),
                  hintStyle: TextStyle(color: Colors.white),
                  icon: const Padding(
                      padding: const EdgeInsets.only(top: 15.0),
                      child: const Icon(
                        Icons.lock_outline,
                        color: Colors.white,
                      )),
                  // errorText:
                  // validate ? 'Password Can\'t Be Empty' : null,
                ),
                obscureText: _obscureText,
              ),
4
Why you doesn't use validator ? you want another way to do it ?Sanjayrajsinh
why not try regex?Mahesh Jamdade

4 Answers

32
votes

Use TextFormField widget which consists of a builtin validator

  // Form
  final GlobalKey<FormState> _form = GlobalKey<FormState>();
  final TextEditingController _pass = TextEditingController();
  final TextEditingController _confirmPass = TextEditingController();

  Form(
        key: _form,
        child: ListView(
              children: <Widget>[
                     TextFormField(
                           controller: _pass,
                           validator: (val){
                              if(val.isEmpty)
                                   return 'Empty';
                              return null;
                              }
                     ),
                      TextFormField(
                           controller: _confirmPass,
                           validator: (val){
                              if(val.isEmpty)
                                   return 'Empty';
                              if(val != _pass.text)
                                   return 'Not Match'
                              return null;
                              }
                     ),
                       ]
              )
    )


    // To validate call
    _form.currentState.validate()
4
votes

Just simply declare a variable first then assign it's value to the value which we are using.

Then compare it down as shown below:

GlobalKey<FormState> _formKey = GlobalKey<FormState>();
      var confirmPass;
    TextFormField(
                                validator: (String value) {
                                  confirmPass = value;
                                  if (value.isEmpty) {
                                    return "Please Enter New Password";
                                  } else if (value.length < 8) {
                                    return "Password must be atleast 8 characters long";
                                  } else {
                                    return null;
                                  }
                                },
                                decoration: InputDecoration(
                                  hintText: "Enter New Password",
                                  hintStyle: TextStyle(color: Colors.grey),
                                  border: new OutlineInputBorder(
                                    borderRadius: const BorderRadius.all(
                                      const Radius.circular(40.0),
                                    ),
                                  ),
                                ),
                              ),
                            ),
                            SizedBox(height: 20),
                            Container(
                              child: TextFormField(
                                validator: (String value) {
                                  if (value.isEmpty) {
                                    return "Please Re-Enter New Password";
                                  } else if (value.length < 8) {
                                    return "Password must be atleast 8 characters long";
                                  } else if (value != confirmPass) {
                                    return "Password must be same as above";
                                  } else {
                                    return null;
                                  }
                                },
                                decoration: InputDecoration(
                                  hintText: "Re-Enter New Password",
                                  hintStyle: TextStyle(color: Colors.grey),
                                  border: new OutlineInputBorder(
                                    borderRadius: const BorderRadius.all(
                                      const Radius.circular(40.0),
                                    ),
                                  ),
                                ),
                              ),
                            ),

1
votes

I did a validation using RxDart and Streams. Although it is a little bit more work I guarantee that the final results improve the performance and also the UX. Check it out on medium

Here is the git hub project

0
votes

This is dart validations for forms you can use as below

  String _password = ''; 
  String _confirmPassword = '';

  final _formKey = GlobalKey<FormState>();

  Form(
    key: _formKey,
    child: Column(
      children: [ 
        const SizedBox(
          height: 30,
        ),
        TextFormField(
          decoration: const InputDecoration(
            hintText: "Password",
          ),
          onChanged: (value){
            _password = value;
          },
          validator: (value) {
            if (value != null && value.isEmpty) {
              return 'Password is required please enter';
            }
            // you can check password length and specifications
            
            return null;
          }
        ),
        const SizedBox(
          height: 30,
        ),
        TextFormField(
          decoration: const InputDecoration(
            hintText: "Confirm Password",
          ),
          onChanged: (value){
            _confirmPassword = value;
          },
          validator: (value) {
            if (value != null && value.isEmpty) {
              return 'Conform password is required please enter';
            }
            if(value != _password){
              return 'Confirm password not matching';
            }
            return null;
          }
        ),
        const SizedBox(
          height: 40,
        ),
        ButtonWidget(
          btnText: "Create an Account",
          onButtonPres: () => {
            // validate all forms fields
            if (_formKey.currentState!.validate()) {
              // do the API call here
            }
          },
        ), 
      ],
    ),
  ),