Some suggestions!! length (username) - returns the length of the string - used i
ID: 3710511 • Letter: S
Question
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.
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.
Explanation / Answer
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
function validateUername() {
var person = prompt("Enter your username", "");
var valid = true;
if(person.length < 8){
valid = false;
}
else{
firstChar = person.substring(0,1);
secondChar = person.substring(1,2);
lastChar = person.substring(person.length-1,person.length);
if((!(firstChar>='a' && firstChar<='z')) || (!(secondChar>='A' && secondChar<='Z')) || !(lastChar>='0' && lastChar <='9'))
{
valid = false;
}
}
if(!valid)
{
document.getElementById("demo").innerHTML =
"Invalid username ";
}else
{
document.getElementById("demo").innerHTML =
"Valid username ";
}
}
</script>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.