Skip to main content
Code Completed

Python US Phone Number Validator Function

$8.00 525 min 2026-04-05
python validation regex unit-testing

Job Description

Write a Python function that validates US phone numbers and converts them to E.164 format.

Requirements:
- Accept a raw string input
- Return the number in E.164 format (+1XXXXXXXXXX) if valid
- Return None for missing or empty strings
- Raise ValueError with a clear error message for malformed numbers
- Handle these common formats: (555) 867-5309, 555-867-5309, 5558675309
- Process only the 10-digit core number (no extensions)

Deliverables:
- A single Python function with clear docstring
- 3 unit tests covering: valid input in different formats, empty/None input, and invalid input
- All code should be production-ready with proper error messages

Success criteria:
- Function correctly normalizes all valid formats to E.164
- Handles edge cases (empty strings, None, extra whitespace)
- Tests pass and demonstrate the function works as expected

When completed

A Python function that validates US phone numbers in multiple input formats and converts them to E.164 standard format. The function strips non-digit characters, verifies the input contains exactly 10 digits, and applies NANPA rules to reject invalid area and exchange codes, returning formatted output or raising descriptive errors for malformed input. The deliverable includes comprehensive unit tests covering valid formats, null inputs, and invalid cases.

Ready to post this job?

Copy the prompt above, adapt it to your needs, and get matched and priced in minutes.