Javascript Cat
Javascript Cat
1. Using valid examples, illustrate 4 ways of incorporating JavaScript into HTML (4 marks)
Embedding code----
<!DOCTYPE html >
<html>
<head>
<title> page title</title>
<script>
document. Write ("Welcome to Java point");
</script>
</head>
<body>
<p>Inthis example we saw how to add JavaScript in the head section </p>
</body>
</html>
Inline code------
<!DOCTYPE html >
<html>
<head>
<title> page title</title>
</head>
<body>
<p>
<a href="#" onClick="alert('Welcome !');">Click Me</a>
</p>
p> in this example we saw how to use inline JavaScript or directly in an HTML tag
</p>
</body>
</html>
External file-----
<html>
<head>
<meta charset="utf-8">
<title>Including a External JavaScript File</title>
</head>
<body>
<form>
<input type="button" value="Result" onclick="display()"/>
</form>
<script src="hello.js">
</script>
</body>
</html>
2. Describe 3 types of JavaScript dialog boxes. For each, show an example (3 marks)
5. Write some JavaScript and HTML code to validate an email address (4 marks)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<div class="mail">
<ul>
<li> </li>
<li> </li>
</ul>
</form>
</div>
<script src="email-validation.js"></script>
</body>
</html>
6. Write JavaScript and HTML code to check if a user entered a numeric value in an ID
umber text box (4 marks)
<script>
function numberValidation() {
event.preventDefault();
var n = document.form.numbers.value;
if (isNaN(n)) {
document.getElementById("numberText")
return false;
} else {
document.getElementById("numberText")
return true;
</script>
<img src=
<span id="numberText"></span>
<br />
<input type="submit" value="submit" />
</form>
7. Write some JavaScript code to validate username and Password entries and ensure the
user has entered details before submitting(3 marks)
<html>
<head>
<title> Verification of valid Password </title>
</head>
<script>
function verifyPassword() {
var pw = document.getElementById("pswd").value;
//check empty password field
if(pw == "") {
document.getElementById("message").innerHTML = "**Fill the password pleae!
";
return false;
}
//minimum password length validation
if(pw.length < 8) {
document.getElementById("message").innerHTML = "**Password length must b
e atleast 8 characters";
return false;
}
//maximum length of password validation
if(pw.length > 15) {
document.getElementById("message").innerHTML = "**Password length mustn
ot exceed 15 characters";
return false;
} else {
alert("Password is correct");
}
}
</script>
<body>
<center>
<h1 style="color:green">Javatpoint</h1>
<h3> Verify valid password Example </h3>
<form onsubmit ="return verifyPassword()">
<!-- Enter Password -->
<td> Enter Password </td>
<input type = "password" id = "pswd" value = "">
<span id = "message" style="color:red"> </span> <br><br>
<!-- Click to verify valid password -->
<input type = "submit" value = "Submit">
<!-- Click to reset fields -->
<button type = "reset" value = "Reset" >Reset</button>
</form>
</center>
</body>
</html>
8. Write JavaScript code to display the following. The JavaScript code must use nested for
loops (4 marks)
<!DOCTYPE html>
<html>
<head>
<meta charset= "utf-8" >
<title>This is an example for nested loop inJavaScript</title>
<head>
<body>
<p>Click below button to loop inner loop each ( 5 ) times for outer loop.</p>
<button onclick="myFunction()">Click Here</button>
<p id="did"></p>
<script>
function myFunction() {
var text = "";
var i;
var j;
for (i = 0; i < 5; i++) {
for (j = 0; j < 2; j++) {
text += "The number is i = " + i + " and j = " + j + "<br>";
}
}
document.getElementById("did").innerHTML = text;
}
</script>
</body>
</html>
*****