0

I'm using RegExp to validate email in one of my Flutter projects. Apparently, I'm able to insert, both by typing and copy-pasting, emojis in the email field. Below is the code for the email field:

Container(
  child: Theme(
    data: ThemeData(
        primaryColor: backgroundColor,
        hintColor: hintTextColor,
        primarySwatch: backgroundColorMaterial),
    child: TextFormField(
      controller: provider.emailController,
      border: outlineBorderGrey,
      hintText: Translations.of(context).getEmail,
      labelText: Translations.of(context).getEmail,
      prefixIcon: IconButton(
        icon: provider.emailvalid ? emailFieldLogo : emailFieldLogoRed,
      ),
    ),
    inputFormatters: [ValidateHelper.emailRegex],
    keyboardType: TextInputType.emailAddress,
  ),
),

);

The inputFormatter that I'm using is also a RegExp:

static TextInputFormatter emailRegex = BlacklistingTextInputFormatter(RegExp(r'[!$#<>?":` ~;[\]\\|=+)(*&^%A-Z]'));

After finishing the form, when I'm applying validation check on the email, the emojis are being accepted as valid input as well. The code for email validation is as follows:

bool isEmailValid(BuildContext context, String email) {
    Pattern patternEmail =
        r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$';
    RegExp regexEmail = new RegExp(patternEmail);
    if (!regexEmail.hasMatch(email)) {
      return false;
    }
    if (email.isEmpty) {
      return false;
    }
    return true;
  }

Is there any problem in the Regex pattern? Did I miss something out?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
pratik97179
  • 133
  • 1
  • 2
  • 12

2 Answers2

0

Emojis are nothing but Characters. A is U+1F601 in Unicode. And that Unicode is allowed with your Regex. You can restrict Here is a question about restricting Emojis in a Textfield. Maybe that helps :)

Chris
  • 1,828
  • 6
  • 40
  • 108
0

Please try this to restrict entering emojis in email text field

inputFormatters: [
              FilteringTextInputFormatter.deny(RegExp(
                  r'(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])')),
            ],
Tejaswini Dev
  • 1,311
  • 2
  • 8
  • 20