Skip to content

Commit 8dba0df

Browse files
committed
Add validations script
1 parent fce5976 commit 8dba0df

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Course3/Lab4/validations.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python3
2+
3+
import re
4+
5+
def validate_user(username, minlen):
6+
"""Checks if the received username matches the required conditions."""
7+
if type(username) != str:
8+
raise TypeError("username must be a string")
9+
if minlen < 1:
10+
raise ValueError("minlen must be at least 1")
11+
12+
# Usernames can't be shorter than minlen
13+
if len(username) < minlen:
14+
return False
15+
# Usernames can only use letters, numbers, dots and underscores
16+
if not re.match('^[a-z0-9._]*$', username):
17+
return False
18+
# Usernames can't begin with a number
19+
if username[0].isnumeric():
20+
return False
21+
return True
22+
23+
24+

0 commit comments

Comments
 (0)