Class PhoneNumberUtility
- Namespace
- Builvero.Application.Utilities
- Assembly
- Builvero.Application.dll
Utility class for normalizing and validating international phone numbers.
public static class PhoneNumberUtility
- Inheritance
-
PhoneNumberUtility
- Inherited Members
Remarks
This utility standardizes phone numbers to E.164 format (+[country code][number]) to ensure consistency and uniqueness across different input formats.
The frontend is responsible for combining country code with phone number before submission. The backend normalizes the combined number to ensure proper format regardless of whether the user entered it with +, 00 prefix, or no prefix.
For comprehensive validation (carrier checks, regional format rules), consider using Google.PhoneNumbers (libphonenumber-csharp).
Methods
IsValidPhoneNumber(string)
Validates if a phone number follows valid E.164 format.
public static bool IsValidPhoneNumber(string phoneNumber)
Parameters
phoneNumberstringThe phone number to validate (should be in E.164 format).
Returns
- bool
True if valid E.164 format, false otherwise.
Remarks
E.164 format: +[country code][number]
- Must start with +
- Followed by 1-15 digits (international standard maximum)
For comprehensive validation (carrier verification, regional rules), use libphonenumber-csharp.
Normalize(string)
Normalizes a phone number to E.164 format (+[country code][number]).
public static string Normalize(string phoneNumber)
Parameters
phoneNumberstringThe phone number to normalize (already combined with country code from frontend).
Returns
- string
Phone number in E.164 format, or empty string if input is null/whitespace.
Remarks
Handles multiple input formats by ensuring a + prefix:
- 00966501234567 (00 prefix) → +966501234567
- 966501234567 (no prefix) → +966501234567
- +966501234567 (already normalized) → +966501234567
Removes common separators (spaces, dashes, parentheses, dots) before normalization.