Last active
January 24, 2019 13:07
-
-
Save chfle/4d996bdce65b3813996674534c9b9924 to your computer and use it in GitHub Desktop.
Python Class for user validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" User validation class""" | |
from abc import ABC, abstractmethod | |
import re | |
class UserValidation(ABC): | |
def __init__(self, username, email, password): | |
self.username = username | |
self.email = email | |
self.password = password | |
@abstractmethod | |
def check_username(self): | |
regex = '^[a-zA-Z0-9]([._](?![._])|[a-zA-Z0-9]){6,18}[a-zA-Z0-9]$' | |
if re.match(regex, self.username): | |
return True | |
raise ValueError('wrong username syntax') | |
@abstractmethod | |
def check_password(self): | |
while True: | |
if 8 > len(self.password): | |
check = -1 | |
break | |
elif not re.search("[a-z]", self.password): | |
check = -1 | |
break | |
elif not re.search("[A-Z]", self.password): | |
check = -1 | |
break | |
elif not re.search("[0-9]", self.password): | |
check = -1 | |
break | |
elif not re.search("[_@$!}[{(0*#%^&]", self.password): | |
check = -1 | |
break | |
elif re.search("\s", self.password): | |
check = -1 | |
break | |
else: | |
check = 0 | |
return True | |
if check == -1: | |
raise ValueError('Password is not secure') | |
@abstractmethod | |
def check_email(self): | |
match = re.search("(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9]+\.[a-zA-Z0-9.]*\.*[com|org|edu]{3}$)", self.email) | |
if match: | |
return True | |
raise ValueError('Email syntax is wrong') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment