1

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 Answer 1

2
  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; }

Sign up to request clarification or add additional context in comments.

2 Comments

Bad regex. Don't use that. Rules out a perfectly valid email of <fred&[email protected]> which for 20 years has been an auto-reply bot for exactly this occasion.
Should I customise the '@example.com' according to the domain names?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.