Phone Number Validation in Python

 Phone Number Validation in Python



How to validate Phone Number in Python

A Python Program to validate phone number

# import re module
import re
# define function for phone number validation
def phvalidation(phone):
    # create pattern
    regxpattern=re.compile("(0|91)?[7-9][0-9]{9}")
    return regxpattern.match(phone)
phone='9'
if (phvalidation(phone)):
    print("The phone number is correct")
else:
    print("The phone number is not correct")


O/P:
The phone number is not correct

A Python program to validate user phone number using regular expressions.
Take User input


# import re module
import re
phone=str(input("Enter phone number\n"))
# define function for phone number validation
def phvalidation(phone):
    # create pattern
    regxpattern=re.compile("(0|91)?[7-9][0-9]{9}")
    return regxpattern.match(phone)
if (phvalidation(phone)):
    print("The phone number is correct")
else:
    print("The phone number is not correct")
    

O/P:
Enter phone number dfgh The phone number is not correct



Another Program

# import re module
import re
phone=str(input("Enter phone number\n"))
# define function for phone number validation
def phvalidation(phone):
    # create pattern
    regxpattern=re.compile("(0|91)?[7-9][0-9]{9}")
    return regxpattern.match(phone)
if (phvalidation(phone)):
    print("The phone number is correct")
else:
    print("The phone number is not correct")

O/P:
Enter phone number 56 The phone number is not correct

टिप्पणी पोस्ट करा

0 टिप्पण्या