-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathPhoneNumberValidator.swift
30 lines (26 loc) · 1.12 KB
/
PhoneNumberValidator.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//
// Copyright 2019 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import Foundation
import SignalServiceKit
struct PhoneNumberValidator {
func isValidForRegistration(phoneNumber: E164) -> Bool {
if phoneNumber.stringValue.hasPrefix("+1") {
return isValidForUnitedStatesRegistration(phoneNumber: phoneNumber)
}
if phoneNumber.stringValue.hasPrefix("+55") {
return isValidForBrazilRegistration(phoneNumber: phoneNumber)
}
// no extra validation for this country
return true
}
private let validBrazilPhoneNumberRegex = try! NSRegularExpression(pattern: "^\\+55\\d{2}9?\\d{8}$", options: [])
private func isValidForBrazilRegistration(phoneNumber: E164) -> Bool {
validBrazilPhoneNumberRegex.hasMatch(input: phoneNumber.stringValue)
}
private let validUnitedStatesPhoneNumberRegex = try! NSRegularExpression(pattern: "^\\+1\\d{10}$", options: [])
private func isValidForUnitedStatesRegistration(phoneNumber: E164) -> Bool {
validUnitedStatesPhoneNumberRegex.hasMatch(input: phoneNumber.stringValue)
}
}