188

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

336

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);
Sign up to request clarification or add additional context in comments.

19 Comments

Best answer here. If you can't identify all valid emails, it is best to be lenient and not frustrate your users.
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.
Among other things this regex won't allow hyphens in domain names: [email protected]
Note [email protected] will NOT work
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]+")
|
130

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",
)

6 Comments

not ";". it is "," isn't it? but yours are a good idea.
Yes,thanks for report.I fixed it
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
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 I cant say its good or not cause I m not guru at regex too :)
|
104

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 Comments

That's the same pattern I pasted, but I didn't mark it as code so it was escaped. If it still doesn't validate email addresses, that's something for the W3C to fix :)
little late, but... It is not work - it`is valid for "s@s" string.
its valid for a@a
While that is the one listed on the page, it allows anything@anything, which seems like a big oversight since as far as I know there are not any valid email addresses of that form.
|
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 Comment

That pattern only accepts TLDs with 3 chars max. There are lots of TLDs bigger than that.
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 Comments

Why is the best answer not this?
It actually works. I've tested it.
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 Comment

Actually you're missing - char for the domain name :) ^.+@[a-zA-Z-]+\.{1}[a-zA-Z]+(\.{0,1}[a-zA-Z]+)$
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 Comment

I like this package, because if I typed test@test it would be considered wrong as I want.
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 Comments

Doesn't allow "[email protected]" (valid) and allows "[email protected]" (invalid).
Do you think [email protected] available to email ?
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 Comments

This doesn't match me@localhost
No it doesn't , just normal emails (use for production for instance)
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.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.