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?
-
6As of 2019: To properly support email validation in Dart/Flutter, please see the pub.dev package email_validatorLance– Lance2019-10-09 20:47:32 +00:00Commented 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_validatorBrett Sutton– Brett Sutton2023-12-12 22:09:59 +00:00Commented Dec 12, 2023 at 22:09
11 Answers
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);
19 Comments
[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.[email protected] will NOT workUsing 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
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
anything@anything, which seems like a big oversight since as far as I know there are not any valid email addresses of that form.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
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
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
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
test@test it would be considered wrong as I want.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
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.
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
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