I need to do pretty basic phone-number validation and formatting on all US and international phone numbers in Python. Here's what I have so far:
import re
def validate(number):
number = re.compile(r'[^0-9]').sub('', number)
if len(number) == 10:
# ten-digit number, great
return number
elif len(number) == 7:
# 7-digit number, should include area code
raise ValidationError("INCLUDE YOUR AREA CODE OR ELSE.")
else:
# I have no clue what to do here
def format(number):
if len(number) == 10:
# basically return XXX-XXX-XXXX
return re.compile(r'^(\d{3})(\d{3})(\d{4})$').sub('$1-$2-$3', number)
else:
# basically return +XXX-XXX-XXX-XXXX
return re.compile(r'^(\d+)(\d{3})(\d{3})(\d{4})$').sub('+$1-$2-$3-$4', number)
My main problem is that I have NO idea as to how international phone numbers work. I assume that they're simply 10-digit numbers with a \d+
of the country code in front of them. Is this true?
E.164 numbers can be up to fifteen digits, and you should have no expectation that beyond the country code of 1-3 digits that they will fit any particular form. Certainly there are lots of countries where it is not XXX-XXX-XXXX. As I see it you have three options:
I ignore the format as in where are the spaces and dashes. But here is the regex function I use to validate that numbers:
def is_valid_phone(phone):
return re.match(r'(\+[0-9]+\s*)?(\([0-9]+\))?[\s0-9\-]+[0-9]+', phone)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With