Home > database >  How to validate email from dummy emails?
How to validate email from dummy emails?

Time:11-17

I am currently working on a project and need validation for validating dummy emails like mailinator or yopmail. These emails should not go through but I can't get any regex for this particular issue.

I have tried different regex but none them worked.

CodePudding user response:

I made a list of all the disposable domains. You can use this list to validate disposable email.

Exp:

final disposableEmail = [
 "xxyxi.com",
 "musiccode.me",
]

final splitList = _emailController.text.split("@");
if (disposableEmail.contains(splitList[1].trim())) {
    print('Registration with temporary-email-address not allowed');
    return;
}

CodePudding user response:

You can use the email validator plugin from the pub.dev to validate email easily https://pub.dev/packages/email_validator

example code

void main() {

    var email = "[email protected]";

    assert(EmailValidator.validate(email));
}

CodePudding user response:

No need of using third-party packages. Just use this function :

String? validateEmail(String? value) {
  if (value != null) {
    if (value.isNotEmpty) {
      String pattern =
          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 regex = RegExp(pattern);
      if (!regex.hasMatch(value)) {
        return 'Enter Valid Email';
      }
    }
  } else if (value == '' || value == null) {
    return null;
  } else {
    return null;
  }
  return null;
}

Now, in your textFormField, you can use this as :

TextFormField(
              autovalidateMode: AutovalidateMode.onUserInteraction,
              decoration: const InputDecoration(
                prefixIcon: Icon(
                  Icons.email,
                ),
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.all(
                    Radius.circular(20),
                  ),
                ),
                hintText: 'Enter your email address.',
              ),
              validator: validateEmail,
            ),
  • Related