-
Notifications
You must be signed in to change notification settings - Fork 163
/
address.go
39 lines (31 loc) · 898 Bytes
/
address.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package emailverifier
import (
"regexp"
"strings"
)
var emailRegex = regexp.MustCompile(emailRegexString)
// Syntax stores all information about an email Syntax
type Syntax struct {
Username string `json:"username"`
Domain string `json:"domain"`
Valid bool `json:"valid"`
}
// ParseAddress attempts to parse an email address and return it in the form of an Syntax
func (v *Verifier) ParseAddress(email string) Syntax {
isAddressValid := IsAddressValid(email)
if !isAddressValid {
return Syntax{Valid: false}
}
index := strings.LastIndex(email, "@")
username := email[:index]
domain := strings.ToLower(email[index+1:])
return Syntax{
Username: username,
Domain: domain,
Valid: isAddressValid,
}
}
// IsAddressValid checks if email address is formatted correctly by using regex
func IsAddressValid(email string) bool {
return emailRegex.MatchString(email)
}