Compare commits
5 Commits
v0.9.0-bet
...
support-ne
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ac2c54aaf2
|
||
|
|
8ceadabbc2
|
||
| 2953f4c2f3 | |||
|
|
57d8b98212
|
||
|
|
bb314d725c
|
@@ -5,7 +5,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
aliasRegex = `^[a-zA-Z0-9]+(\.[a-zA-Z0.9]+)*$`
|
aliasRegex = `^(?i)[a-z][a-z0-9]*(\.[a-z0-9]+)*$`
|
||||||
)
|
)
|
||||||
|
|
||||||
// IsValidAlias checks if the alias is a valid alias format
|
// IsValidAlias checks if the alias is a valid alias format
|
||||||
|
|||||||
@@ -9,7 +9,10 @@ import (
|
|||||||
// Define the regex patterns as constants
|
// Define the regex patterns as constants
|
||||||
const (
|
const (
|
||||||
// TODO: This should rather use a phone package to determine whether valid phone number for any region.
|
// TODO: This should rather use a phone package to determine whether valid phone number for any region.
|
||||||
phoneRegex = `^(?:\+254|254|0)?((?:7[0-9]{8})|(?:1[01][0-9]{7}))$`
|
// Kenyan phone numbers: must be exactly 10 digits (07XXXXXXXX or 01XXXXXXXX) when starting with 0
|
||||||
|
// Or start with 254 / +254 and still follow the same pattern
|
||||||
|
// Supports any 01 prefix (010, 011, 014, 015, etc.) for future adaptability
|
||||||
|
phoneRegex = `^(?:\+254|254|0)(7\d{8}|1\d{8})$`
|
||||||
)
|
)
|
||||||
|
|
||||||
// IsValidPhoneNumber checks if the given number is a valid phone number
|
// IsValidPhoneNumber checks if the given number is a valid phone number
|
||||||
@@ -39,3 +42,34 @@ func FormatPhoneNumber(phone string) (string, error) {
|
|||||||
|
|
||||||
return "+" + phone, nil
|
return "+" + phone, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FormatToLocalPhoneNumber converts a phone number like "+2547XXXXXXXX"
|
||||||
|
// or "2547XXXXXXXX" into the local format "07XXXXXXXX" / "01XXXXXXXX".
|
||||||
|
func FormatToLocalPhoneNumber(phone string) (string, error) {
|
||||||
|
// Remove leading "+" and spaces
|
||||||
|
phone = strings.TrimPrefix(phone, "+")
|
||||||
|
phone = strings.ReplaceAll(phone, " ", "")
|
||||||
|
|
||||||
|
// Must start with 254
|
||||||
|
if !strings.HasPrefix(phone, "254") {
|
||||||
|
return "", errors.New("invalid international phone format")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove "254" prefix → get 7XXXXXXXX or 1XXXXXXXX
|
||||||
|
rest := phone[3:]
|
||||||
|
|
||||||
|
// Validate: must be 9 digits
|
||||||
|
if len(rest) != 9 {
|
||||||
|
return "", errors.New("invalid phone number length")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Kenyan mobile numbers start with 7 or 1 (Safaricom/Airtel/Telkom prefixes)
|
||||||
|
if !(strings.HasPrefix(rest, "7") || strings.HasPrefix(rest, "1")) {
|
||||||
|
return "", errors.New("invalid Kenyan mobile prefix")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert to local: prepend "0"
|
||||||
|
local := "0" + rest
|
||||||
|
|
||||||
|
return local, nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user