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

Time:03-07

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 :

             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';
                  }
                },
              ),

CodePudding user response:

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)) {

CodePudding user response:

You can try using \^[A-z] $\.

  • Related