How should I check if the input is a email address with dart?

Dart

Dart Problem Overview


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?

Dart Solutions


Solution 1 - Dart

For that simple regex works pretty good.

var email = "[email protected]"
bool emailValid = RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+").hasMatch(email);

Solution 2 - Dart

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])?)*$

Solution 3 - Dart

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

Solution 4 - Dart

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.

Solution 5 - Dart

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.

Solution 6 - Dart

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

Solution 7 - Dart

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)

Solution 8 - Dart

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

Solution 9 - Dart

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.

Solution 10 - Dart

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.

Solution 11 - Dart

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEric LavoieView Question on Stackoverflow
Solution 1 - DartAiron TarkView Answer on Stackoverflow
Solution 2 - DartJustin FagnaniView Answer on Stackoverflow
Solution 3 - Dart763View Answer on Stackoverflow
Solution 4 - DartEric LavoieView Answer on Stackoverflow
Solution 5 - DartDavid_EView Answer on Stackoverflow
Solution 6 - DartPriyansh BrannenView Answer on Stackoverflow
Solution 7 - DartLanceView Answer on Stackoverflow
Solution 8 - DartGooglianView Answer on Stackoverflow
Solution 9 - DartMajidView Answer on Stackoverflow
Solution 10 - DartbitfreezeView Answer on Stackoverflow
Solution 11 - Dartuser11496192View Answer on Stackoverflow