Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sundowndev
GitHub Repository: sundowndev/phoneinfoga
Path: blob/master/lib/number/utils.go
988 views
1
package number
2
3
import (
4
"regexp"
5
"strconv"
6
7
phoneiso3166 "github.com/onlinecity/go-phone-iso3166"
8
)
9
10
// FormatNumber formats a phone number to remove
11
// unnecessary chars and avoid dealing with unwanted input.
12
func FormatNumber(n string) string {
13
re := regexp.MustCompile(`[_\W]+`)
14
number := re.ReplaceAllString(n, "")
15
16
return number
17
}
18
19
// ParseCountryCode parses a phone number and returns ISO country code.
20
// This is required in order to use the phonenumbers library.
21
func ParseCountryCode(n string) string {
22
var number uint64
23
number, _ = strconv.ParseUint(FormatNumber(n), 10, 64)
24
25
return phoneiso3166.E164.Lookup(number)
26
}
27
28
// IsValid indicate if a phone number has a valid format.
29
func IsValid(number string) bool {
30
number = FormatNumber(number)
31
32
re := regexp.MustCompile("^[0-9]+$")
33
34
return len(re.FindString(number)) != 0
35
}
36
37