Validate email address in Dart?

天涯浪子 提交于 2019-12-06 17:16:02

问题


According to RegExp documentation, we must use "JavaScript" (Perl 5) regular expressions : Ecma Specification. What pattern do you use for email validation?


回答1:


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



回答2:


For that simple regex works pretty good.

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



回答3:


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.




回答4:


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.




回答5:


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("aslam@gmail.com"));
}

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




回答6:


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. user+filter@domain.name)
  • Domainless addresses (e.g. user@localhost)



回答7:


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, me@email.com but not me@localhost. Hope it helps.




回答8:


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.



来源:https://stackoverflow.com/questions/16800540/validate-email-address-in-dart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!