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

My RegExp does not work correctly ,it must be just letters

I searched a lot of sites about RegExp patterns that match only letters
but I can enter numbers with letters in TextFormField Widget and validate and it says nothing (means it is true) but
in fact It must say ‘Country is not valid’,
suggest me best pattern for my RegExp.

example : Italy12dd says correct but in fact I want just letters.

My code :

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

             TextFormField(
                autovalidateMode: AutovalidateMode.onUserInteraction,
                decoration: const InputDecoration(
                    label: Text('Country'),
                    border: OutlineInputBorder(),
                    labelStyle: TextStyle(fontSize: 17),
                    errorStyle: TextStyle(fontSize: 13)),
                keyboardType: TextInputType.name,
                validator: (String? value) {
                  if (value!.isEmpty) {
                    return 'Country must not be empty';
                  } else if (!RegExp(r'[a-zA-Z]+$').hasMatch(value)) {
                    return 'Enter valid Country';
                  }
                },
              ),

>Solution :

It looks to me like your regex is looking for something that ends in one or more letters and that’s it. If you want only letters, then you need to specify that it’s only letters from beginning to end. I don’t know flutter so it’s hard to recreate to be sure, but try adding a ^ at the beginning to indicate the beginning of the line:

                  } else if (!RegExp(r'^[a-zA-Z]+$').hasMatch(value)) {
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