Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TextFormFied validate spacing

I have TextFormField widget with username

TextFormField(
                controller: usernameController,
                validator: (value) {
                  if (value!.isEmpty) {
                    return 'username is empty';
                  }
                  if (value.length <= 4) {
                    return 'passord id too short, min 4 characters';
                  }
                  return null;
                },
                decoration: const InputDecoration(
                    hintText: 'username',
                    border: OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.black))),
              ),

But I want to take username to database as one word. How to validate spacing TextFormField

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can split the string on space and count the list length.

validator: (value) {
  if (value!.isEmpty) {
    return 'username is empty';
  }
    if (value.trim().split(" ").length > 1) { // trim for single word extra space, you can remove it if needed
    return 'cant be multi word';
  }
  if (value.length <= 4) {
    return 'passord id too short, min 4 characters';
  }
  return null;
},
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading