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?
CodePudding user response:
Emojis are nothing but Characters. A