-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidators.go
68 lines (50 loc) · 1.19 KB
/
validators.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
66
67
68
package lwhelper
import (
"fmt"
"regexp"
"strings"
"golang.org/x/net/idna"
)
var (
emailRegex = regexp.MustCompile(`^([a-zA-Z0-9.!#$%&*+/=?^_{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*)?$`)
digitsRegex = regexp.MustCompile(`\D`)
)
func ValidEmail(s string) bool {
return emailRegex.MatchString(s)
}
func ValidDomain(s string) bool {
_, err := idna.Lookup.ToASCII(s)
return err == nil
}
func ValidDomainOrWildcard(s string) bool {
return ValidDomain(strings.TrimPrefix(s, "*."))
}
func GetDomainBaseName(domainName string) string {
t := strings.Split(domainName, ".")
if len(t) < 2 {
return ""
}
tld := 2
if strings.HasSuffix(domainName, ".gov.pl") ||
strings.HasSuffix(domainName, ".com.pl") ||
strings.HasSuffix(domainName, ".co.uk") ||
strings.HasSuffix(domainName, ".com.br") {
tld = 3
}
//
return strings.Join(t[len(t)-tld:], ".")
}
//
func ValidPhone(s, countryPrefix string) string {
// usuwam wszystko co nie jest cyfra
s = digitsRegex.ReplaceAllString(s, "")
// usuwam prefix
s = strings.TrimPrefix(s, countryPrefix)
// 000 000 000
if len(s) == 9 {
// nr jest poprawny
return fmt.Sprintf("+%s %s", countryPrefix, s)
}
// nr jest NIEpoprawny
return ""
}