173k views
4 votes
You have been asked to write a username validation program for a small website. The website has specific rules on what constitutes a valid username, including:

All usernames must be between 8 and 15 characters long

Usernames can only contain alphabetic (a-z and A-Z) and numeric characters (0-9) - no special characters are allowed.

The first character in a username cannot be a digit
Usernames must contain at least one uppercase character
Usernames must contain at least one lowercase character
Usernames must contain at least one numeric character

Write a program that asks the user to enter in a username and then examines that username to make sure it complies with the rules above. Here's a sample running of the program - note that you want to keep prompting the user until they supply you with a valid username:

a.Please enter a username: foo
Username must be between 8 and 15 characters.

b.Please enter a username: fooooooooooooooooooo
Username must be between 8 and 15 characters.

c.Please enter a username: foo ooo ooo
Username must contain only alphanumeric characters.

d.Please enter a username: foooooooooo
Your username must contain at least one digit

e.Please enter a username: 1fooooooooo
The first character in your username cannot be a digit

f.Please enter a username: fooooooooo1
Your username must contain at least one uppercase character

g.Please enter a username: Fooooooooo1
Your username is valid!

User Robgraves
by
6.2k points

1 Answer

3 votes

Answer:

#function to validate username

def valid_username(username):

#checking is length is between 8 to 15

length_valid=True

if(len(username)<8 or len(username)>15):

length_valid=False;

#checking is string is alphanumeric

alphanumeric=username.isalnum()

#checking first_and_last Characters of string is not digit

first_and_last=True

if(username[0].isdigit() or username[len(username)-1].isdigit()):

first_and_last=False

#counting uppercase lowercase and numeric Characters in string

uppercase=0

lowercase=0

numeric=0

for i in username:

if(i.isdigit()):

numeric=numeric+1

elif(i.isalpha()):

if(i.isupper()):

uppercase=uppercase+1

else:

lowercase=lowercase+1

valid_no_char=True

if(uppercase<1 or lowercase<1 or numeric<1):

valid_no_char=False

#printing the result

print("Length of username: "+str(len(username)))

print("All Characters are alpha-numeric: "+str(alphanumeric))

print("First and last Character are not digit: "+str(first_and_last))

print("no of uppercase characters in the username: "+str(uppercase))

print("no of lowercase characters in the username: "+str(lowercase))

print("no of digits in the username: "+str(numeric))

#checking string is Valid or not

if(length_valid and alphanumeric and first_and_last and valid_no_char):

print("Username is Valid\\")

return True

else:

print("username is invalid, please try again\\")

return False

#loop runs until valid_username enterd by user

while(True):

username=input("Enter a username: ")

if(valid_username(username)):

break

Step-by-step explanation:

The code was created by considering all the required cases asked in the question.

In this program, I asked username from the user continuously until it enters a valid username

first it check for length of string

then check is string contains only alphanumeric characters

then first and last digit is or not

than no of uppercase lowercase and numeric character

using the above parameter declare username valid or not

You have been asked to write a username validation program for a small website. The-example-1
User UnR
by
6.4k points