34 lines
1.0 KiB
Swift
34 lines
1.0 KiB
Swift
enum GiftContactInputRule {
|
|
case name
|
|
case phoneNumber
|
|
|
|
func acceptsEditing(_ text: String) -> Bool {
|
|
switch self {
|
|
case .name:
|
|
return text.count <= 12 && text.unicodeScalars.allSatisfy { scalar in
|
|
switch scalar.value {
|
|
case 0x41...0x5A, 0x61...0x7A,
|
|
0x1100...0x11FF, 0x3130...0x318F,
|
|
0xA960...0xA97F, 0xAC00...0xD7A3, 0xD7B0...0xD7FF:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
case .phoneNumber:
|
|
return text.count <= 11 && text.unicodeScalars.allSatisfy {
|
|
(0x30...0x39).contains($0.value)
|
|
}
|
|
}
|
|
}
|
|
|
|
func acceptsSubmission(_ text: String) -> Bool {
|
|
switch self {
|
|
case .name:
|
|
return !text.isEmpty && acceptsEditing(text)
|
|
case .phoneNumber:
|
|
return text.count == 11 && text.hasPrefix("010") && acceptsEditing(text)
|
|
}
|
|
}
|
|
}
|