forked from Nixius/authelia
40 lines
869 B
Go
40 lines
869 B
Go
package validation
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
var emailRe = regexp.MustCompile(`^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$`)
|
|
|
|
// SanitizeEmail trims, lowercases, and validates email format. Returns empty if invalid.
|
|
func SanitizeEmail(input string) string {
|
|
s := strings.TrimSpace(strings.ToLower(input))
|
|
if s == "" || !emailRe.MatchString(s) || len(s) > 254 {
|
|
return ""
|
|
}
|
|
return s
|
|
}
|
|
|
|
// SanitizePhone normalizes to E.164-like format: + followed by 10-15 digits.
|
|
// Accepts (555) 123-4567, +1 555 123 4567, etc.
|
|
func SanitizePhone(input string) string {
|
|
var digits strings.Builder
|
|
hasPlus := false
|
|
for _, r := range input {
|
|
if r == '+' {
|
|
hasPlus = true
|
|
} else if r >= '0' && r <= '9' {
|
|
digits.WriteRune(r)
|
|
}
|
|
}
|
|
d := digits.String()
|
|
if len(d) < 10 || len(d) > 15 {
|
|
return ""
|
|
}
|
|
if hasPlus {
|
|
return "+" + d
|
|
}
|
|
return "+" + d
|
|
}
|