Home > Blockchain >  how can you validate an email as invalid when the email don't have the .com in flutter?
how can you validate an email as invalid when the email don't have the .com in flutter?

Time:04-01

I'm wondering, can you validate an email without the .com as invalid in Flutter

Example:
[email protected] = true
johnny@johnny    = false

CodePudding user response:

What I use for my email validators is this:

String? validateEmail(String email) {
    const String 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,}))$';
    final RegExp regex = RegExp(pattern);
    if (email.isEmpty || !regex.hasMatch(email)) {
      return 'Invalid email';
    } else {
      return null;
    }
  }

hope it helps.

Example:
[email protected] = true
[email protected]    = true
johnny@johnny    = false
johnny@    = false
johnny    = false
[email protected]    = false

CodePudding user response:

there are many sources to help you do that, you can test email_validator package or use regex like this, i think its better to share your code and errors! there are many ways to do that ,just search

CodePudding user response:

Try the following code you can import this class in to the page you want u can just validate any String like following

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

eg:if(anyString.isValidEmail()) { print("Valid")}

  • Related