Last active
July 16, 2019 03:47
-
-
Save Crazy3lf/2ad282843d89521959911fdc853d51ed to your computer and use it in GitHub Desktop.
email validation code for Go. For test cases, refer https://gist.github.com/Crazy3lf/5cd87ae25fb21c92284b04201f1ffc5e
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func isValidEmail(Value string) (result bool) { | |
result = false | |
var checkAllowed = regexp.MustCompile(`^[a-zA-z0-9_\-.+]+$`).MatchString | |
i := strings.Index(Value, `@`) | |
if i < 1 { | |
return | |
} | |
if strings.Contains(Value, `..`) || strings.Contains(Value, `@@`) || strings.Contains(Value, `.@`) { | |
return | |
} | |
if strings.IndexAny(Value, `.@`) == 0 { | |
return | |
} | |
namePart := Value[:i] | |
serverPart := Value[i+1:] | |
if len(namePart) == 0 || len(serverPart) < 5 { | |
return | |
} | |
// namePart surrounded by '"' is valid | |
if strings.HasPrefix(namePart,`"`) && strings.HasSuffix(namePart,`"`){ | |
strings.Trim(namePart,`"`) | |
} | |
// serverPart cannot start with '-' | |
if strings.HasPrefix(serverPart,`-`){ | |
return | |
} | |
// check the position of '.' | |
i = strings.Index(serverPart, `.`) | |
if i <= 0 || i > len(serverPart)-2{ | |
return | |
} | |
result = checkAllowed(namePart) && checkAllowed(serverPart) | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment