Signal-iOS/Signal/Registration/PhoneNumberValidator.swift
Max Radermacher f4e8ccd3bd
Parse phone numbers using country codes
Co-authored-by: Harry <109690906+harry-signal@users.noreply.github.com>
2024-12-11 14:52:27 -06:00

31 lines
1.1 KiB
Swift

//
// 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)
}
}