Skip to content
Snippets Groups Projects
Commit 472327eb authored by Johan de Klerk's avatar Johan de Klerk
Browse files

ValidateEmailAddress

parent 269f7280
Branches
Tags
No related merge requests found
......@@ -3,6 +3,8 @@ package string_utils
import (
"encoding/json"
"fmt"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors"
"net/mail"
"net/url"
"regexp"
"strconv"
......@@ -307,11 +309,18 @@ func StripQueryString(inputUrl string) (string, error) {
return u.String(), nil
}
func IsValidEmail(email string) bool {
// Found here: https://stackoverflow.com/questions/201323/how-can-i-validate-an-email-address-using-a-regular-expression
regex, _ := regexp.Compile("(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])")
isValidEmail := regex.MatchString(email)
return isValidEmail
func ValidateEmailAddress(email string) (string, error) {
if email == "" {
return "", errors.Error("email address is empty")
}
// We validate it but still return it since in some cases we don't want to break everything if the email is bad
_, err := mail.ParseAddress(email)
if err != nil {
return strings.ToLower(strings.TrimSpace(email)), errors.Wrap(err, "could not parse email address")
}
return strings.ToLower(strings.TrimSpace(email)), nil
}
func PascalCaseToSentence(pascal string) string {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment