-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidateFields.py
113 lines (75 loc) · 4.41 KB
/
validateFields.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""
Validate specific fields like username and password
Input the data and output the error
"""
from tabnanny import check
from validationChecks import presenceCheck, checkAllowedCharacters, lengthCheck, checkIfStringContainsLettersAndNumbers, checkIfValueIsUsed, formatCheck
# Presence check and allowed characters check
def validateUsername(username) :
feedbackText = ""
if checkAllowedCharacters("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM-1234567890", username) == "FAIL" :
feedbackText = "Sorry, you can only enter alphabetical characters, numerical characters, and -"
if presenceCheck(username) == "FAIL" :
feedbackText = "Sorry, you need to enter your username"
if checkIfValueIsUsed(username, "database/users.csv", 1, True) == "FAIL" :
feedbackText = "Sorry, this username has already been used!"
return feedbackText
# Allowed characters, presence, and value used check
def checkChatroomName(chatroomName) :
feedbackText = ""
if checkAllowedCharacters("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM-1234567890 ¬`£$!%^&*()_=+[]:;@'~#<>.?/|", chatroomName) == "FAIL" :
feedbackText = "Sorry, you can only enter alphabetical characters, numerical characters, and some special characters"
if presenceCheck(chatroomName) == "FAIL" :
feedbackText = "Sorry, you need to enter the chatroom name"
if checkIfValueIsUsed(chatroomName, "database/chatrooms.csv", 2, False) == "FAIL" :
feedbackText = "Sorry, this name has already been used!"
return feedbackText
# Allowed characters, presence, and value used check
def validateFlashcardName(flashcardName) :
feedbackText = ""
if checkAllowedCharacters("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM-1234567890 ¬`£$!%^&*()_=+[]:;@'~#<>.?/|", flashcardName) == "FAIL" :
feedbackText = "Sorry, you can only enter alphabetical characters, numerical characters, and some special characters"
if presenceCheck(flashcardName) == "FAIL" :
feedbackText = "Sorry, you need to enter the flashcard name"
if checkIfValueIsUsed(flashcardName, "database/flashcards.csv", 1, False) == "FAIL" :
feedbackText = "Sorry, this name has already been used!"
return feedbackText
# Presence check
# Allowed character check not done because the langauge could be written in any language, so it can't easily be checked
def validateChatroomLanguage (language) :
feedbackText = ""
if presenceCheck(language) == "FAIL" :
feedbackText = "Sorry, you need to enter the chatroom name"
return feedbackText
# Presence check, length check, allowed characters check, double entry and characters and numbers check
def validatePassword(password, passwordConfirmation) :
feedbackText = ""
if checkAllowedCharacters("!£$%^&*()_-+={]}[<>.\|qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM-1234567890", password) == "FAIL" :
feedbackText = "Sorry, you can only enter alphabetical characters, numerical characters, and a few special characters"
if lengthCheck(password, 8) == "FAIL" :
feedbackText = "Sorry, your password must be longer than 8 characters"
if checkIfStringContainsLettersAndNumbers(password) == "FAIL" :
feedbackText = "Sorry, your password must contain both letters and numbers"
if presenceCheck(password) == "FAIL" :
feedbackText = "Sorry, you need to enter your password"
if password != passwordConfirmation :
feedbackText = "Sorry, your two passwords do not match"
return feedbackText
# check allowed characters and format check
def validateEmail(email) :
feedbackText = ""
# Allow the email to be blank, otherwise it will fail the format check
if email == "" :
return feedbackText
if checkAllowedCharacters("@!£$%^&*()_-+={]}[<>.\|qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM-1234567890", email) == "FAIL" :
feedbackText = "Sorry, you can only enter alphabetical characters, numerical characters, and a few special characters"
if formatCheck(email, r'^\S+@\S+\.\S+$') == "FAIL" :
feedbackText = "Sorry, you need to enter a valid email address"
return feedbackText
# Check if flashcard does not contain ","
def validateFlashcard(flashcard) :
feedbackText = ""
if flashcard != "":
if "," in flashcard or "/" not in flashcard:
feedbackText = "Sorry, you can't use a comma in your flashcard"
return feedbackText