Fix the phone number check behind a sign-up form

Review the AI's codeEasy10 min

Your task

An AI wrote a 40-character regex to check phone numbers at sign-up. It passed every number the team tried, but nobody could read it, and it was about to gate every new user. Your team replaced it with a plain check whose rules fit on one screen, and tests it with inputs at every boundary.

An assistant wrote validate_phone_numbers(raw_numbers). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • raw_numbers: phone numbers as people typed them.
  • Return true or false for each number, in order.

Rules the tests check. A number is valid only when all of these hold:

  • An optional + comes first. A + anywhere else makes the number invalid.
  • After that, every character is a digit from 0 to 9, a space or a -. Anything else, such as a letter, a bracket or an extension marker, makes it invalid.
  • It has 8 to 15 digits in total. Spaces and hyphens do not count.
  • With a leading +, the first digit must not be 0, because country codes never start with 0. Without a +, a leading 0 is fine: many countries write national numbers that way.

Examples

validate_phone_numbers(raw_numbers: list[str]) -> list[bool]
Example 1
Inputvalidate_phone_numbers(["+14155552671", "415-555-2671", "+1-800-FLOWERS"])
Output[True, True, False]
Example 2
Inputvalidate_phone_numbers([""])
Output[False]
Example 3
Inputvalidate_phone_numbers(["+1 415 555 2671", "+0123456789", "+"])
Output[True, False, False]