Home > Back-end >  How to detect whitespaces in flutter input field
How to detect whitespaces in flutter input field

Time:01-04

I have been trying to detect whitespaces in my flutter app's input field. I've made a custom input field in which I am taking controller and I'm putting validation on it. I've put all the validations successfully but I am unable to detect whitespaces either with regex. I don't want to trim the string I just want to detect and validate the user error message if the user has entered whitespace in the username. Here's my code even though its not necessary but I'm putting it anyway

InputText(
                                  onTap: () {
                                    FocusScopeNode currentFocus =
                                        FocusScope.of(context);

                                    if (!currentFocus.hasPrimaryFocus &&
                                        currentFocus.focusedChild != null) {
                                      FocusManager.instance.primaryFocus
                                          ?.unfocus();
                                    }
                                  },
                                  controller: userName,
                                  buttonText: "*Username",
                                ),

Validations:

  void vaildation() async {
    if (userName.text.isEmpty && email.text.isEmpty && password.text.isEmpty) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text("Please fill in fields to sign up"),
          duration: Duration(milliseconds: 600),
        ),
      );
    } else if (email.text.isEmpty) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text("Please enter an email"),
          duration: Duration(milliseconds: 600),
        ),
      );
    } else if (!regExp.hasMatch(email.text)) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text("Please enter valid email"),
          duration: Duration(milliseconds: 600),
        ),
      );
    } else if (password.text.isEmpty) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text("Please enter a password"),
          duration: Duration(milliseconds: 600),
        ),
      );
    } else if (phoneNumber.text.isEmpty) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text("Please enter your phone number"),
          duration: Duration(milliseconds: 600),
        ),
      );
    } else {
      submit();
    }
  }

Validation check:

AppButton(
                                  buttonText: "Sign up",
                                  onTap: () {
                                    vaildation();
                                  },
                                ),

CodePudding user response:

In your validation function, there is no where you actually try to validate the username. Add the following to check for whitespace in the username.

else if (RegExp(r"\s").hasMatch(username.text)) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text("Please enter valid username"),
          duration: Duration(milliseconds: 600),
        ),
      );
    }

CodePudding user response:

When you pass value in submit function

userName.text.trim() 
email.text.trim() 
password.text.trim()

trim() is used for remove white pass in text value

  • Related