This commit is contained in:
lash
2025-01-21 15:59:44 +00:00
parent ba8cbbccea
commit 9fbae0471f
5 changed files with 9 additions and 9 deletions

View File

@@ -8,7 +8,8 @@ import (
// Define the regex patterns as constants
const (
phoneRegex = `^(?:\+254|254|0)?((?:7[0-9]{8})|(?:1[01][0-9]{7}))$`
// 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}))$`
)
// IsValidPhoneNumber checks if the given number is a valid phone number
@@ -20,7 +21,7 @@ func IsValidPhoneNumber(phonenumber string) bool {
// FormatPhoneNumber formats a Kenyan phone number to "+254xxxxxxxx".
func FormatPhoneNumber(phone string) (string, error) {
if !IsValidPhoneNumber(phone) {
return "", errors.New("invalid phone number")
return "", errors.New("invalid phone number")
}
// Remove any leading "+" and spaces
@@ -29,11 +30,11 @@ func FormatPhoneNumber(phone string) (string, error) {
// Replace leading "0" with "254" if present
if strings.HasPrefix(phone, "0") {
phone = "254" + phone[1:]
phone = "254" + phone[1:]
}
// Add "+" if not already present
if !strings.HasPrefix(phone, "254") {
return "", errors.New("unexpected format")
return "", errors.New("unexpected format")
}
return "+" + phone, nil