Class PhoneHelper
- Namespace
- SecureExchangesSDK.Helpers
- Assembly
- SecureExchangesSDK.NetCore.dll
The PhoneHelper class provides utility methods for validating, normalizing, and manipulating phone numbers. It includes methods for:
- Validating phone numbers against regular expressions and using the PhoneNumberUtil library.
- Checking if phone numbers can receive SMS messages.
- Normalizing phone numbers by removing common formatting characters.
- Adding a country code to national phone numbers if not already present.
This class aims to ensure that phone numbers are in a consistent and valid format before performing operations on them.
public static class PhoneHelper
- Inheritance
-
PhoneHelper
- Inherited Members
Methods
AddCountryCodeToNationalNumber(string, string)
Adds the country code to a national phone number if not already present.
public static string AddCountryCodeToNationalNumber(string phoneNumber, string countryCode)
Parameters
Returns
- string
The phone number with the country code added, or the original phone number if an error occurs.
IsValidPhoneNumber(string, params int[])
Validates the format of a phone number using regular expressions and checks its validity using the PhoneNumberUtil library. The method also checks if the input string is null or empty, returning false if it is. If no number types are specified, it defaults to validating mobile, fixed-line or mobile, and fixed-line phone numbers.
public static bool IsValidPhoneNumber(string phoneNumber, params int[] numberTypes)
Parameters
phoneNumberstringThe phone number to validate.
numberTypesint[]Optional parameter to specify acceptable phone number types as int values. Defaults to mobile, fixed-line or mobile, and fixed-line phone numbers if not specified. Acceptable int values: 0 - FIXED_LINE 1 - MOBILE 2 - FIXED_LINE_OR_MOBILE 3 - TOLL_FREE 4 - PREMIUM_RATE 5 - SHARED_COST 6 - VOIP 7 - PERSONAL_NUMBER 8 - PAGER 9 - UAN 10 - VOICEMAIL 11 - UNKNOWN
Returns
- bool
True if the phone number is not null/empty and is valid according to the regular expressions and PhoneNumberUtil, false otherwise.
IsValidPhoneNumberAndCanReceiveSMS(string, string)
Checks if a phone number can receive SMS messages. The method first checks if the input is not null or empty. It then parses the phone number and checks its type, and validates the phone number's format and overall validity.
public static bool IsValidPhoneNumberAndCanReceiveSMS(string phoneNumber, string countryCode = null)
Parameters
phoneNumberstringThe phone number to check.
countryCodestringThe country code to add if the phone number is national.
Returns
- bool
True if the phone number is not null/empty, is valid, and can receive SMS messages, false otherwise.
NormalizePhone(string)
Normalizes a phone number by removing spaces, dashes, and parentheses.
public static string NormalizePhone(string phoneNumber)
Parameters
phoneNumberstringThe phone number to normalize.
Returns
- string
The normalized phone number.
ValidatePhoneNumber(string, string, string, out string)
Validates the phone number format for a specific region and country code.
[Obsolete("This method is obsolete. Use IsValidPhoneNumber instead.")]
public static bool ValidatePhoneNumber(string region, string countryCode, string phoneNumber, out string finalPhoneNumber)
Parameters
regionstringThe region code.
countryCodestringThe country code.
phoneNumberstringThe phone number to validate.
finalPhoneNumberstringThe validated phone number with the country code added.
Returns
- bool
True if the phone number is valid for the specified region and country code, False otherwise.