How to validate email in all the possible ways in Flutter TextFormField

85 views Asked by At

I want to validate the email in all possible ways. I've checked many posts on Stackoverflow, and most of the answers suggest using Regex, but it does not validate domain names. For example,if I enter [email protected], it validates as the correct one. I also checked the email_validator package, which also did not give the solution. Is there any method to solve my problem?

I look forward to your answers.

1

There are 1 answers

2
Dewa Prabawa On
  1. email_validator: ^2.1.17

    import 'package:email_validator/email_validator.dart';

     bool isValidEmail(String email) {
       if (!EmailValidator.validate(email)) {
         return false;
       }
    
       if (!email.endsWith('@example.com')) {
         return false;
       }
       return true;
     }
    
  2. Regex.

    bool isValidEmail(String email) {

    final emailRegex = RegExp( r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$', );

    if (!emailRegex.hasMatch(email)) { return false; }

    if (!email.endsWith('@example.com')) { return false; }

    return true; }