-
Notifications
You must be signed in to change notification settings - Fork 0
/
email.go
65 lines (55 loc) · 1.87 KB
/
email.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package is
import (
"regexp"
"strings"
)
var (
// Regular expression pattern for a valid email slug (local/domain part).
slugPattern = `[\p{L}\p{N}]{1,}([\p{L}\p{N}.-]{1,}[\p{L}\p{N}]{1,})?`
// Compiled regex for the local part of the email.
localRegex = regexp.MustCompile(`^` + slugPattern + `$`)
// Compiled regex for the domain part of the email.
domainRegex = regexp.MustCompile(`^` + slugPattern + `\.[\p{L}\p{N}]{2,}$`)
)
// Email validates an email address and returns a boolean result.
// The function checks for email length, splits the email into local and
// domain parts, validates the length of the local part, and finally uses
// regular expressions to check the format of each part.
//
// Example usage:
//
// is.Email("test@example.com") // Output: true
// is.Email("TEST@EXAMPLE.COM") // Output: true
// is.Email(" test@example.com") // Output: false
// is.Email("test@example.c") // Output: false
//
// The function performs only a format check, so it does not clean spaces
// at the beginning and end of a line, does not remove tab characters and
// carriage returns to a new line. You can use the g.Wedd and g.Trim
// functions for it:
//
// is.Email(g.Trim(" test@example.com\n")) // Output: true
// is.Email(g.Weed("test\t@example.com\n")) // Output: true
func Email(email string) bool {
// Convert to lowercase to handle case sensitivity.
email = strings.ToLower(email)
// Check the overall length.
if len(email) > 254 || len(email) < 6 {
return false
}
// Split the email into local and domain parts.
parts := strings.Split(email, "@")
if len(parts) != 2 {
return false
}
local, domain := parts[0], parts[1]
// Check the length of the local part.
if len(local) > 64 || len(local) < 1 {
return false
}
// Check if both parts are valid.
if !localRegex.MatchString(local) || !domainRegex.MatchString(domain) {
return false
}
return true
}