Javascript Create a web page that will use a prompt box and ask a user to enter
ID: 3739853 • Letter: J
Question
Javascript
Create a web page that will use a prompt box and ask a user to enter a valid user name. The entire user name will be accepted as a single String in the JavaScript code.
A valid user name will contain at least 8 characters (test for length first). The first character must be a lower case letter of the alphabet (a - z), the second character must be an upper case letter of the alphabet (A-Z), and the last character must be a digit (0-9). Refer to pages 342 and 343 for pre-existing JavaScript string functions to help test for these conditions. Some suggestions!!
length(username) - returns the length of the string - used in JavaScript code as username.length (see page 344)
use the statement firstchar = substring(username, 0, 1) - this returns to the variable firstchar the first character of the username
to test for A - Z use if statements to see if the char is >=A and <=Z
to test for a - z use if statements to see if the char is >=a and <=z
Test the last character with isNAN() - this function returns true if the character is NOT a number and false if it is a number OR test if the character is >=1 and <=9
Your JavaScript code will test for these conditions on the user entered string and indicate on the Web page if a valid user name was entered.
give the user 3 chances to enter a correct user name. If after 3 chances, a correct user name is not entered then print a message on the Web page and end the script.
Explanation / Answer
<script>
function fun() {
var count = 0;
while(count < 3) {
var username = prompt("Enter the password");
if(username.length >= 8) {
var firstchar = username.substring(0, 1);
var secondchar = username.substring(1, 2);
if((firstchar >= 'a' && secondchar <= 'z') && (secondchar >= 'A' && secondchar <= 'Z')) {
var lastchar = username.substring(username.length - 1, username.length);
if(!isNaN(lastchar)) {
alert("valid user name is entered");
break;
}
}
}
count++;
}
if(count >= 3)
alert("Maximum tries exceeded");
}
</script>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.