Skip to content

Instantly share code, notes, and snippets.

@aslamanver
Created September 25, 2019 12:45
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aslamanver/3a3389b8ef88831128f0fa21393d70f0 to your computer and use it in GitHub Desktop.
Save aslamanver/3a3389b8ef88831128f0fa21393d70f0 to your computer and use it in GitHub Desktop.
Email Validation in Dart
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"));
}
@shinriyo
Copy link

shinriyo commented Jul 5, 2020

Why don't you do this? Any reason?
return (!regex.hasMatch(value)) ? false : true;
=>
return regex.hasMatch(this);

@aslamanver
Copy link
Author

Why don't you do this? Any reason?
return (!regex.hasMatch(value)) ? false : true;
=>
return regex.hasMatch(this);

That's also one of the ways.

@shinriyo
Copy link

shinriyo commented Aug 7, 2020

@aslamanver
yes, it is. perhaps, the negative check syntax is faster than the positive check in the computer science?

@kushande
Copy link

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

Why using pattern "The argument type 'Pattern' can't be assigned to the parameter type 'String'."

@sethyanita
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment