Phone Utilities
Utility functions for validating, normalizing, and formatting phone numbers with support for Saudi Arabian phone number formats.normalizePhone
Normalizes and validates phone numbers to E.164 format. Handles Saudi Arabian phone numbers with special logic for local formats.Parameters
The phone number string to normalize. Can contain spaces, dashes, and parentheses.
Returns
Whether the phone number is valid
The normalized phone number in E.164 format (e.g., +966512345678). Only present if valid is true.
Error message if validation failed. Only present if valid is false.
Behavior
- Removes spaces, dashes, and parentheses from input
- Converts Saudi local format (05XXXXXXXX) to international format (+9665XXXXXXXX)
- Handles 00966 prefix and converts to +966
- Validates against E.164 format (7-15 digits after country code)
Examples
The function specifically handles Saudi Arabian phone numbers (country code +966) with special normalization logic for local formats starting with 05.
formatPhoneForDisplay
Formats a phone number for display purposes. Currently supports Saudi Arabian numbers with local format display.Parameters
The phone number in E.164 format (e.g., +966512345678)
Returns
Returns the formatted phone number string. For Saudi numbers, converts to local format with spaces (e.g., “0512 345 678”). For other numbers, returns the original input.Examples
This function is useful for displaying phone numbers in a user-friendly format. It converts Saudi international format back to the familiar local format with spacing.