Reputation: 95
I want to write code to give username and password from a user and check the format of the email. Email's format is expression(string or number or both)@string.string. If the email's format is correct I must enter this email and password into a table in my database. Could you help me to write the correct code, my code does not work?
import re
import mysql.connector
cnx = mysql.connector.connect(user='root', password='',
host='127.0.0.1',
database='username_password')
cursor = cnx.cursor()
print("Enter email address: ")
email=input()
print("enter password: ")
password=input()
regex = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'
def check(email):
if(re.search(regex,email)):
return("Valid Email")
else:
print("Invalid Email")
print("Enter correct format: [email protected]")
print("Enter email address: ")
email=input()
if __name__ == '__main__' :
check(email)
if check(email)=="Valid Email":
cursor.execute("INSERT INTO _info Values (email,password)")
cnx.commit()
Upvotes: 0
Views: 331
Reputation: 54168
You need to have a method who's job is well defined, for now it can return a string, or input a new email, one option is that the method only return a vlaid email
Also check how to insert in db, you need to pass the values at some point
def get_valid_email():
regex = r'^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'
email = input("Enter email address: ")
while not re.fullmatch(regex, email):
print("Invalid Email")
print("Enter correct format: [email protected]")
email = input("Enter email address: ")
return email
if __name__ == '__main__':
cnx = mysql.connector.connect(user='root', password='',
host='127.0.0.1',
database='username_password')
cursor = cnx.cursor()
email = get_valid_email()
password = input("enter password: ")
cursor.execute("INSERT INTO _info Values (%s,%s)", (email, password))
cnx.commit()
Upvotes: 2