Links

Validate email and phone numbers

Tags: #python #twilio #project #call #mobile #snippet
Last update: 2023-06-05 (Created: 2023-06-05)
Description: This notebook validates a given email address and phone number using re and phonenumbers modules.
References:

Input

Import librairies

import re
try:
import phonenumbers
except ModuleNotFoundError:
!pip install phonenumbers
import phonenumbers

Setup Variables

You can build and test regular expression patterns using this regex101.com
  • email_regex: Pattern to match Email Address
email_regex = "^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$" # Pattern to match Email Address

Model

Check email function

def check_email(email):
if re.fullmatch(email_regex, email):
print("Valid Email✅")
else:
print("Invalid Email❌")

Check phone number function

def check_phone_number(phone_number):
phone_number = phonenumbers.parse(phone_number)
possible = phonenumbers.is_valid_number(phone_number)
if possible:
if phonenumbers.is_possible_number(phone_number):
print("Valid Phone Number✅")
else:
print("Invalid Phone Number❌")

Output

Check email and phone number

input_email = input("Enter the Email-ID: ")
check_email(input_email)
input_phone_number = input("Enter the Phone Number (please start with your country ID like +33 for France): ")
check_phone_number(input_phone_number)