skip to Main Content

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?

2

Answers


  1. 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');
    
    Login or Signup to reply.
  2. 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 '';
          }
        
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search