Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a1391e6

Browse files
author
Amogh Singhal
authored
Merge pull request devAmoghS#17 from Sameirquotb/username-validation-enhancement
Update username_validation.py
2 parents e26bd34 + 8303084 commit a1391e6

File tree

1 file changed

+14
-33
lines changed

1 file changed

+14
-33
lines changed

‎username_validation.py‎

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,29 @@
1010
def UsernameValidation(strParam):
1111

1212
# username is between 4 and 25 characters
13-
if len(strParam) <= 25 and len(strParam) >= 4:
14-
flag1 = True
15-
else:
16-
flag1 = False
13+
if len(strParam) < 4 or len(strParam) > 25 :
14+
return False
1715

1816
# start with a letter
19-
if str(strParam[0]).isalpha():
20-
flag2 = True
21-
else:
22-
flag2 = False
23-
24-
# contains only letters, numbers and underscore
25-
valid_grammar = "abcdefghijklmnopqrstuvwxyz0123456789_"
26-
27-
for char in strParam:
28-
if str(char).isalpha() == False:
29-
if char in valid_grammar:
30-
flag3 = True
31-
else:
32-
flag3 = False
33-
34-
else:
35-
if str.lower(char) in valid_grammar:
36-
flag3 = True
37-
else:
38-
flag3 = False
39-
17+
if not str(strParam[0]).isalpha():
18+
return False;
4019

4120
# can't end with an underscore
42-
if str(strParam[-1]) != '_':
43-
flag4 = True
44-
else:
45-
flag4 = False
21+
if str(strParam[-1] ) == '_':
22+
return False;
23+
24+
# contains only letters, numbers and underscore
25+
valid_grammar = set('abcdefghijklmnopqrstuvwxyz0123456789_')
4626

47-
final_output = flag1 and flag2 and flag3 and flag4
27+
for ch in strParam:
28+
if ch.lower() not in valid_grammar:
29+
return False;
4830

49-
# code goes here
50-
return final_output
31+
return True
5132

5233
# keep this function call here
5334
TC1 = "aa_"
54-
TC2 = "u__hello_world123"
35+
TC2 = "uaa__hello_worldW"
5536

5637
print(TC1, UsernameValidation(TC1))
5738
print(TC2, UsernameValidation(TC2))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /