181

According to RegExp documentation, we must use JavaScript (Perl 5) regular expressions : ECMA Specification. What method should I use in Dart to check if the input is an email?

2
  • 6
    As of 2019: To properly support email validation in Dart/Flutter, please see the pub.dev package email_validator
    – Lance
    Commented Oct 9, 2019 at 20:47
  • As of 2023 there is a well supported and popular package that provides the best solution: onepub.dev/packages/email_validator Commented Dec 12, 2023 at 22:09

11 Answers 11

329

For that simple regex works pretty good.

const String email = "[email protected]"

final bool emailValid = 
    RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+")
      .hasMatch(email);
18
  • 9
    Best answer here. If you can't identify all valid emails, it is best to be lenient and not frustrate your users. Commented Dec 31, 2018 at 10:48
  • 5
    For people looking at this as the highest upvoted answer at time of posting, this does not accept [email protected] which is how many users will automatically filter emails in their inbox. So don't use this unless you want to annoy them. If you want the most lenient regex, then use the accepted answer. Simple !== best.
    – DF_
    Commented Jul 22, 2019 at 23:14
  • 6
    Among other things this regex won't allow hyphens in domain names: [email protected] Commented Oct 31, 2019 at 16:30
  • 7
    Note [email protected] will NOT work
    – chuang
    Commented Apr 27, 2021 at 18:05
  • 10
    Cool but I own a valid email: [email protected] so the dash will not pass. This will allow '-' in the word after the '@' RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9-]+\.[a-zA-Z]+")
    – Gal Rom
    Commented May 10, 2021 at 13:07
127

Using the RegExp from the answers by Eric and Justin,
I made a extension method for String:

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

TextFormField(
  autovalidate: true,
  validator: (input) => input.isValidEmail() ? null : "Check your email",
)
5
  • not ";". it is "," isn't it? but yours are a good idea.
    – shinriyo
    Commented Jun 14, 2020 at 0:06
  • Yes,thanks for report.I fixed it
    – 763
    Commented Jun 14, 2020 at 21:35
  • Perfeect, Dart Regexp doesn't work by copy pasting the regex from HTML5, your code has the fixes it needs to work properly, thank you for that! :D Commented Feb 19, 2021 at 20:26
  • For anyone coming in the year 2023+ this, in my personal opinion (not that anyone asked, but still :D) might be the best solution for an e-mail validator for dart. @763 will correct me if I'm wrong since I'm no regex guru, but on the last part of the regex, you can constrain the length of the domain (i.e.+[a-zA-Z] {2, 5 } ), because the current regex allows you to enter 2> characters (.comcomcomcom, .netnet, .etcetcetc). Hope it helps. Cheers!
    – GrandMagus
    Commented Feb 20, 2023 at 18:07
  • @GrandMagus I cant say its good or not cause I m not guru at regex too :)
    – 763
    Commented Feb 25, 2023 at 11:59
100

I'd recommend everyone standardize on the HTML5 email validation spec, which differs from RFC822 by disallowing several very seldom-used features of email addresses (like comments!), but can be recognized by regexes.

Here's the section on email validation in the HTML5 spec: http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#e-mail-state-%28type=email%29

And this is the regex:

^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?)*$
8
40

I use this pattern : validate-email-address-in-javascript. (Remove slash / delimiters and add the Dart delimiters : r' ').

bool isEmail(String em) {

  String p = 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 regExp = new RegExp(p);

  return regExp.hasMatch(em);
}

EDIT :

For more information on email validation, look at these posts : dominicsayers.com and regular-expressions.info . This tool may also be very useful : gskinner RegExr.

EDIT : Justin has a better one. I'm using the pattern he proposed.

1
  • 1
    That pattern only accepts TLDs with 3 chars max. There are lots of TLDs bigger than that.
    – karliwson
    Commented Jan 21, 2019 at 19:09
25

The best regEx pattern I've found is the RFC2822 Email Validation:

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

Taken from: regexr.com/2rhq7

All the other regEx I've tested, mark the string email@email as a positive, which is false.

2
  • Why is the best answer not this?
    – Sheychan
    Commented Aug 29, 2019 at 13:15
  • It actually works. I've tested it. Commented Nov 7, 2022 at 17:23
14

I used a simple and not so rigorous validator which also allows [email protected] and [email protected] domains:

var email = "[email protected]";
  bool emailValid = RegExp(r'^.+@[a-zA-Z]+\.{1}[a-zA-Z]+(\.{0,1}[a-zA-Z]+)$').hasMatch(email);
  print (emailValid);
1
  • 1
    Actually you're missing - char for the domain name :) ^.+@[a-zA-Z-]+\.{1}[a-zA-Z]+(\.{0,1}[a-zA-Z]+)$ Commented May 10, 2020 at 21:50
13

2019 Correct Answer

To properly support email validation in Dart/Flutter, please see the pub.dev package email_validator.

Source: https://github.com/fredeil/email-validator.dart

_

This properly supports:

  • TLDs [optionally]
  • International Domains [optionally]
  • Filtered domains (e.g. [email protected])
  • Domainless addresses (e.g. user@localhost)
1
  • 1
    I like this package, because if I typed test@test it would be considered wrong as I want.
    – Tayan
    Commented Aug 21, 2023 at 6:37
10

Email validation in Dart, follow the Regex:

bool validateEmail(String value) {
  Pattern 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 = new RegExp(pattern);
  return (!regex.hasMatch(value)) ? false : true;
}

void main() {
  print(validateEmail("[email protected]"));
}

Flow the below Regex:

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,}))$'

Reference: https://gist.github.com/aslamanver/3a3389b8ef88831128f0fa21393d70f0

2
2

I have seen this page a few times when I was searching, and I came up with a simpler Regex for dart which might help those who will come to this page later.

here is the regex:

^[^@]+@[^@]+\.[^@]+

so in dart you can use it like

RegExp(r'^[^@]+@[^@]+\.[^@]+')

It only supports normal emails and not without TLD. for instance, [email protected] but not me@localhost. Hope it helps.

2
  • This doesn't match me@localhost
    – Toto
    Commented Sep 18, 2019 at 11:55
  • No it doesn't , just normal emails (use for production for instance)
    – Majid
    Commented Sep 18, 2019 at 12:03
0

I have arrived to this page in search for e-mail validation, but none of the examples found here have passed all my tests.

Therefore I decided to write my own regEx, adapting some of the concepts from other answers (standing on shoulders of giants), and it is doing great so far:

^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9][a-zA-Z0-9-]{0,253}\.)*[a-zA-Z0-9][a-zA-Z0-9-]{0,253}\.[a-zA-Z0-9]{2,}$

If you find any issues with that pattern, please let me know.

0

The best regular expression i've came across till now is the following:

r'([a-z0-9][-a-z0-9_+.][a-z0-9])@([a-z0-9][-a-z0-9.][a-z0-9].(com|net)|([0-9]{1,3}.{3}[0-9]{1,3}))'

this approach relies on adding every domain name you want your user to be able to log in with.

i just added com and net since they are the most popular ones but you can simply add more

Not the answer you're looking for? Browse other questions tagged or ask your own question.