209k views
3 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:

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

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

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

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

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

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

Please enter a username: Fooooooooo1
Your username is valid!

User Divieira
by
5.3k points

1 Answer

7 votes

Answer:

var username; // username entered by user

var charAny; // text character identified in username

var anyNum = false; // digit variable used to detect whether the username has one or not

var index; // index loop variable

var BR = "<br />"; //break

var ES = ""; //space

// Display program requirements for the username requested

document.write("We'll begin helping you select a username" + BR);

document.write("Your username must have at least 8 characters," + BR);

document.write(" start with a letter, and contain at least 1 numeric character." + BR);

username = prompt("Please enter your username: ", ES);

// Check for length of username

while (username.length < 8) {

document.write("Your username must be at least 8 characters long." + BR);

username = prompt("Please enter your username: ", ES);

}

// Check that first character is a letter

// Substring function has three arguments: string, starting position, and ending position

charAny = username.substr(0, 1);

while (charAny !== isLetter()) {

document.write("The first character of your username must be a letter." + BR);

username = prompt("Please enter your username: ", ES);

}

// Check that there's at least one digit in the username

while (anyNum !== false) {

// Check each character, set anyNum to true if a digit

for (index = 1; index < username.substr(index, index); index++) {

anyNum = username.substr(index, index);

if (isNumeric(charAny)) {

anyNum = true;

}

}

// If anyNum is false there were no numerics

if (anyNum !== true) {

document.write("Your username must include at least 1 digit." + BR);

username = prompt("Please enter your username: ", ES);

}

}

User Richard YS
by
4.7k points