Home > Enterprise >  username input must be in English only in flutter form
username input must be in English only in flutter form

Time:12-27

In my form validation I already added stuff like the username already exists or it should be longer than n letters etc. but now I want to add more restrictions and display a message that says 'username' is invalid when it's not in English or if it has stuff like underscores slashes etc. is there a way for me to do that?

CodePudding user response:

For just the English Alphabets you need to set a regex for the alphabetical pattern checking. Create a static final field for the RegExp to avoid creating a new instance every time a value is checked.

static final RegExp alphaExp = RegExp('[a-zA-Z]'); 

And then to use it :

validator: (value) => value.isEmpty 
    ? 'Enter Your Name'
    : (alphaExp.hasMatch(value) 
        ? null 
        : 'Only Alphabets are allowed in a username');

CodePudding user response:

you can set textfields validations like this it will only except characters a to z. and add validation in textfield through this

class FormValidator {
      static String validateEmail(String? name) {
        if (name!.isEmpty) {
          return 'Name is must not e empty';
        }
        String pattern =
            '([a-zA-Z])';
        RegExp regExp = RegExp(pattern);
        if (!regExp.hasMatch(name)) {
          return 'invalid name';
        }
        return '';
      }
    
    }
  • Related