0

I was looking around and found the re.match to validate a user's input on a phone number. I have the print function there to debug, and it keeps coming back as None type instead of True. Is there a formatting error that I am overlooking?

def validation():
    flag = True
    while flag:
        phone = input('Enter your phone number (XXX-XXX-XXXX): ')
        answer = re.match(r'^[0-9]{3}-[0-9]{3}-[0-9]}{4}$',phone)
        print(answer)

Basically, I am trying to get the function to validate that the number is in the format XXX-XXX-XXXX and when it is then I can continue, but right now all I am focusing on is returning True on the re.match. The rest of the code I will do after.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Benji
  • 21
  • 2

1 Answers1

2

You have mistyped '}' in your regex.

Your solution looks correct. This will be the regex to match the phone number format.

re.match(r'^[0-9]{3}-[0-9]{3}-[0-9]{4}$',phone)

And you can write the below code for validation of the phone number and return the boolean value from the function.

import re
def validation(phone):
    answer = re.match(r'^[0-9]{3}-[0-9]{3}-[0-9]{4}$',phone)
    if(answer is not None):
        return True
    return False
    

phone = input('Enter your phone number (XXX-XXX-XXXX): ')
print(validation(phone))