1

I'm wondering, can you validate an email without the .com as invalid in Flutter

Example:
johnny@gmail.com = true
johnny@johnny    = false
  • Have you tried to google search about this make sure to search on google if you not get answer then only ask Question here. – Vishal Parmar Mar 31 '22 at 08:55
  • @VishalParmar I tried to search it and I found someone else stack, but the regex doesn't validate if the email have or don't have the .com. So I want to make sure by asking a question – Monster Eat Mar 31 '22 at 09:03

2 Answers2

4

What I use for my email validators is this:

String? validateEmail(String email) {
    const 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,}))$';
    final RegExp regex = RegExp(pattern);
    if (email.isEmpty || !regex.hasMatch(email)) {
      return 'Invalid email';
    } else {
      return null;
    }
  }

hope it helps.

Example:
johnny@gmail.com = true
johnny@johnny.co    = true
johnny@johnny    = false
johnny@    = false
johnny    = false
johnny@johnny.c    = false
0

Try the following code you can import this class in to the page you want u can just validate any String like following

extension EmailValidator on String {
     bool isValidEmail() {
        return RegExp(
           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,}))$')
      .hasMatch(this);
                     }
               }
      

eg:if(anyString.isValidEmail()) { print("Valid")}

MhdBasilE
  • 356
  • 1
  • 4
  • 13