-
Notifications
You must be signed in to change notification settings - Fork 0
/
validators.py
163 lines (147 loc) · 4.88 KB
/
validators.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from termcolor import colored
def get_numeric(placeholder, min_value=None, max_value=None):
if min_value is not None and max_value is not None and max_value < min_value:
raise ValueError(
colored("min_value must be less than or equal to max_value.", "red")
)
while True:
user_input = input(placeholder)
try:
user_input = int(user_input)
except ValueError:
print(colored("Input type must be in int.", "red"))
continue
if min_value == max_value:
print(colored("Input must be equal to {0}.".format(max_value), "red"))
elif max_value is not None and user_input > max_value:
print(
colored(
"Input must be less than or equal to {0}.".format(max_value), "red"
)
)
elif min_value is not None and user_input < min_value:
print(
colored(
"Input must be greater than or equal to {0}.".format(min_value),
"red",
)
)
else:
return user_input
def get_phone_number(placeholder):
while True:
phone_number = input(placeholder)
if len(phone_number) < 6 or len(phone_number) > 14:
print(colored("phone number must be between 6 and 14 digits", "red"))
continue
return phone_number
def get_password(placeholder):
numbers_count = 0
capital_letters_count = 0
while True:
password = input(placeholder)
for char in password:
if char.isnumeric():
numbers_count += 1
elif char.isupper():
capital_letters_count += 1
if len(password) < 8:
print(colored("password must have at least 8 letters", "red"))
elif numbers_count == 0:
print(colored("password must have at least 1 number", "red"))
elif capital_letters_count == 0:
print(colored("password must have at least 1 capital letter", "red"))
else:
return password
def get_name(placeholder):
while True:
invalid_string = False
name = input(placeholder)
for char in name:
if char.isnumeric():
print(colored("name must be in letters only", "red"))
invalid_string = True
break
if char.isspace():
print(colored("name characters must be continous", "red"))
invalid_string = True
break
if invalid_string:
continue
return name
def get_user_name(placeholder):
while True:
invalid_user_name = False
user_name = input(placeholder)
for char in user_name:
if char.isspace():
print(colored("user name must be wriiten without spaces", "red"))
invalid_user_name = True
break
if char.isnumeric() or char.isalpha() or char == "_":
continue
else:
print(
colored(
"user name supports only underscores '_' as a special character",
"red",
)
)
invalid_user_name = True
break
if invalid_user_name:
continue
return user_name
def get_address(placeholder):
while True:
invalid_address = False
address = input(placeholder)
for char in address:
if (
char.isnumeric()
or char.isalpha()
or char.isspace()
or char == "_"
or char == "-"
or char == "."
or char == ","
):
continue
else:
invalid_address = True
print(
colored(
"address supports only alphabet characters and '-', '_', ',' or '.' as special characters",
"red",
)
)
break
if invalid_address:
continue
return address
def get_role(placeholder):
while True:
role = input(placeholder)
if (
role != "member"
and role != "Member"
and role != "admin"
and role != "Admin"
):
print(
colored(
"member and admin roles are only supported",
"red",
)
)
continue
return role.lower()
def get_credit(placeholder):
while True:
user_input = input(placeholder)
try:
user_input = int(user_input)
except ValueError:
print(colored("Input type must be in int.", "red"))
continue
return user_input