Skip to main content

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.
function normalizePhone(input: string): PhoneValidationResult

Parameters

input
string
required
The phone number string to normalize. Can contain spaces, dashes, and parentheses.

Returns

valid
boolean
required
Whether the phone number is valid
normalized
string
The normalized phone number in E.164 format (e.g., +966512345678). Only present if valid is true.
error
string
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

// Saudi local format
const result1 = normalizePhone("0512345678")
// { valid: true, normalized: "+966512345678" }

// International format
const result2 = normalizePhone("+966 51 234 5678")
// { valid: true, normalized: "+966512345678" }

// With 00 prefix
const result3 = normalizePhone("00966512345678")
// { valid: true, normalized: "+966512345678" }

// Invalid format
const result4 = normalizePhone("123")
// { valid: false, error: "Invalid phone number format" }
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.
function formatPhoneForDisplay(phone: string): string

Parameters

phone
string
required
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

// Saudi number
const display1 = formatPhoneForDisplay("+966512345678")
// "0512 345 678"

// Non-Saudi number (returned as-is)
const display2 = formatPhoneForDisplay("+12025551234")
// "+12025551234"
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.

TypeScript Interface

export interface PhoneValidationResult {
  valid: boolean
  normalized?: string
  error?: string
}

Build docs developers (and LLMs) love