order validation
This commit is contained in:
parent
2a4110ee8c
commit
de22166113
6 changed files with 164 additions and 7 deletions
57
orders/phone_validation.go
Normal file
57
orders/phone_validation.go
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
package orders
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func isValidPolishMobile(nineDigitNum string) bool {
|
||||
if len(nineDigitNum) != 9 {
|
||||
return false
|
||||
}
|
||||
|
||||
firstDigit := nineDigitNum[0]
|
||||
|
||||
if firstDigit < '4' || firstDigit > '8' {
|
||||
return false
|
||||
}
|
||||
|
||||
prefix2 := nineDigitNum[:2]
|
||||
validPrefixes := map[string]bool{
|
||||
"45": true,
|
||||
"50": true, "51": true, "53": true, "57": true,
|
||||
"60": true, "66": true, "69": true,
|
||||
"72": true, "73": true, "78": true, "79": true,
|
||||
"88": true,
|
||||
}
|
||||
|
||||
return validPrefixes[prefix2]
|
||||
}
|
||||
|
||||
func parseAndValidatePolishMobile(input string) (string, error) {
|
||||
input = strings.TrimSpace(input)
|
||||
|
||||
if strings.HasPrefix(input, "+48") {
|
||||
input = strings.TrimPrefix(input, "+48")
|
||||
} else if strings.HasPrefix(input, "0048") {
|
||||
input = strings.TrimPrefix(input, "0048")
|
||||
}
|
||||
|
||||
re := regexp.MustCompile(`\D`)
|
||||
cleaned := re.ReplaceAllString(input, "")
|
||||
|
||||
if len(cleaned) == 11 && strings.HasPrefix(cleaned, "48") {
|
||||
cleaned = cleaned[2:]
|
||||
}
|
||||
|
||||
if len(cleaned) != 9 {
|
||||
return "", fmt.Errorf("invalid length: expected 9 digits, got %d", len(cleaned))
|
||||
}
|
||||
|
||||
if !isValidPolishMobile(cleaned) {
|
||||
return "", fmt.Errorf("invalid mobile prefix: %s does not match Polish mobile allocations", cleaned[:2])
|
||||
}
|
||||
|
||||
return cleaned, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue