Search code examples
regexflutterdartemail-verification

Flutter: How to specify exact character to appear during email validation


In my email validation, I want to make it mandatory that the user should use the full .com extension in there email field. Currently, the way I have my RegExp it only accepts starting with .c or .o or .m instead of throwing an error if there in no exact match with .com

How do I go about resolving this?

Here is my RegExp

final RegExp emailValidatorRegExp = RegExp(r"^[a-zA-Z0-9.]+@[a-zA-Z0-9]+.com");

Solution

  • It can check domains like [email protected] and [email protected]

    void main() {
      var email = "[email protected]";
      bool emailValid = RegExp(r'^.+@[a-zA-Z]+\.{1}[a-zA-Z]+(\.{0,1}[a-zA-Z]+)$').hasMatch(email);
      print (emailValid); // true
    }