Skip to content

Instantly share code, notes, and snippets.

@nathan-cruz77
Last active September 7, 2019 03:04
Show Gist options
  • Save nathan-cruz77/06e8c4f263f22fcec4777dd117063e9b to your computer and use it in GitHub Desktop.
Save nathan-cruz77/06e8c4f263f22fcec4777dd117063e9b to your computer and use it in GitHub Desktop.
E.164 phone number validator
import re
# Validates whether a phone number is in E.164 (https://en.wikipedia.org/wiki/E.164)
# format.
#
# Sample usage:
#
# >>> is_e164('55 (12) 98154-1281')
# True
#
# >>> is_e164('800 555 2233')
# True
#
# >>> is_e164('0800 123 4567')
# False
#
def is_e164(number):
exp = re.compile('\+?[1-9]\d{1,14}')
number = ''.join(c for c in number if c.isdigit())
return exp.match(number) is not None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment