Tutorial J9
Tutorial J9
Aim: To learn about basic JavaScript and using forms and validating forms
Name: Tan Wan Sean_____________________________ Date:
_________________
1. Open a text editor of your choice, such as Notepad (on Windows) or TextEdit (on a Mac).
2. Create an HTML page leaving space between <body> and </body> tags.
3. Between the <body> and </body> tags add the <script> and </script> tags.
<!DOCTYPE html>
<html>
<head>
<script>
function add()
{
var a,b,c
a=Number(document.getElementById("first").value);//get first value from form
b=Number(document.getElementById("second").value);//get second value
from form
c= a + b;
document.getElementById("answer").value=c;//puts answer in the form
}
</script>
</head>
<body>
<form id="info_form">
<label>First number:</label><input type="text" id="first"><br/>
<label>Second number:</label><input type="text"id="second"><br/>
<label>Answer:</label><input type="text" input id="answer"><br/>
</form>
<button onclick="add()">Add</button>
</body>
</html>
6. We will return to the numbers guessing game again but this time using a form ID method
for input. First create the form as follows:
<div class="form">
<label for="guessField">Enter a guess: </label>
<input type = "text" id = "guessField" class = "guessField">
<input type = "submit" value = "Submit guess"
class = "guessSubmit" id = "submitguess">
</div>
7. Next create the code for the game:
<script type = "text/javascript">
document.getElementById("submitguess").onclick = function()
{
if(x == y)
{
alert("CONGRATULATIONS!!! YOU GUESSED IT RIGHT IN "
+ guess + " GUESS ");
}
else if(x > y) /* if guessed number is greater
than actual number*/
{
guess++;
alert("OOPS SORRY!! TRY A SMALLER NUMBER");
}
else
{
guess++;
alert("OOPS SORRY!! TRY A GREATER NUMBER")
}
}
</script>
The correct number is 9, if you key in the number 9, it will pops up and say “
congratulation
9. Try out this form validation code using form name method:
<!DOCTYPE html>
<html>
<head>
<script>
function validateForm()
{
var x = document.forms["myForm"]["fname"].value;
if (x == "") {
alert("Name must be filled out");
return false;
}
}
</script>
</head>
<body>
</body>
</html>
10. Create an HTML page with a form and a text box with the label text "Phone Number
(XXX-XXX-XXXX):" and an id of "phone".
<body>
<form id="getphone">
<label for="phone">Phone Number (XXX-XXX-XXXX):</label>
<input type="text" id="phone"><br><br>
<input type="submit" value="Submit">
</form>
<script>
//Write your validation code for the telephone number here
</script>
</body>
11. Create a validation code for the telephone number input in the code above.
12. Try the following 2 codes and see what is the difference:
<p>Click the button to locate the last occurrence <p>Click the button to locate the last occurrence
of a specified value.</p> of a specified value.</p>
<script> <script>
function myFunction() function myFunction()
{ {
var str = "Hello planet earth, you are a great var str = "Hello planet earth, you are a great
planet."; planet.";
var n = str.lastIndexOf("planet"); var n = str.indexOf ("planet");
document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML =
n; n;
} }
</script> </script>
13. Validating an email address. The following example shows a simple method (not
necessary the best) to validate an entered email address using an array using indexOf()
and lastIndexOf().
An email address must contain at least a ‘@’ sign and a dot (.). Also, the ‘@’ must not be
the first character of the email address, and the last dot must at least be one character after
the ‘@’ sign. For example: a.b@c.c and a@b.c are valid emails. 1.1 and a@b are invalid
emails for example.
<script>
var emailID = prompt("Enter a valid email address");
atpos = emailID.indexOf("@"); //number of chars before '@'
dotpos = emailID.lastIndexOf("."); // number of chars before the last '.'
Insert your condition, test and validate the code. Your code:
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
text = document.getElementById("p01").innerHTML;
document.getElementById("demo").innerHTML = /e/.test(text);
</script>
</body>
</html>