Challenge 6

☎️

Validating Phone Numbers

Open in Code Editor or Copy Instructions
...solutions at bottom of page...

Create a function that can take in a format string and a phone number - the function will return a boolean for whether the phone number is/isn't in the valid format

"D" can be used to represent a digit

Examples:

validate("(DDD) DDD-DDDD", "(123) 456-7890") would return TRUE

validate("+DD DDD-DDD-DDD", "(123) 456-7890") would return FALSE

Extension:

Allow a variable number of characters to simulate e.g. different countries having longer/shorter country codes - this could be specified with the square brackets "D[DD]" which would be interpreted as "at least one digit...and up to an additional 2 extra"

Solutions

IGCSE 0478 / O-Level 2210 A-Level 9618