Lab 5
Lab 5
COOKIES
THEORY
JavaScript is a loosely-typed language. It does not give compile-time errors. So
some times you will get a runtime error for accessing an undefined variable or
calling undefined function etc.
JavaScript provides error-handling mechanism to catch runtime errors
using try-catch-finally block.
try
{
// code that may throw an error
}
catch(ex)
{
// code to be executed if an error occurs
}
finally{
// code to be executed regardless of an error occurs or not
}
The try statement allows you to define a block of code to be tested for errors
while it is being executed.
Form Validation
JavaScript provides a way to validate form's data on the client's computer before
sending it to the web server. Form validation generally performs two functions.
Basic Validation − The form must be checked to make sure all the mandatory
fields are filled in. It would require just a loop through each field in the form
and check for data.
Data Format Validation − The data that is entered must be checked for correct
form and value. Your code must include appropriate logic to test correctness of
data.
JavaScript Cookies
A cookie is an amount of information that persists between a server-side and a
client-side. A web browser stores this information at the time of browsing.
QUESTIONS
1.Illustrate the concept of runtime error handling in JavaScript. Make proper use
of try…catch statement, throw statement and finally statement.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible"
content="IE=edge" />
<meta name="viewport" content="width=device-
width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script src="q1.js"></script>
<button onclick="try_block()">try block</button>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible"
content="IE=edge" />
<meta name="viewport" content="width=device-
width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script src="q1.js"></script>
<button onclick="try_block()">try block</button>
</body>
</html>
OUTPUT
function validate(){
let
username=document.getElementById("username").value;
let
usercode=document.getElementById("usercode").value;
let
Mobilenumber=document.getElementById("mobile").value;
let gender=document.getElementsByName("gender");
let pat1=/^@[0-9]{4}[a-z A-Z]{4}#$/;
let pat2=/^98[0-9]{8}$/;
if(username=="")
{
alert("Enter username.");
return false;
}
else if(!(gender[0].checked ||
gender[1].checked))
{
alert("Select a gender.")
return false;
}
else if (!(pat1.test(usercode))){
alert("Invalid code.");
return false;
}
else if (!(pat2.test(Mobilenumber))){
alert("Invalid number.");
return false;
}
else
{
alert("Form submitted successfully.")
}
}
// else if(Province=="")
// {
// alert("Select a province.");
// return false;
// }
3.Illustrate the concept of cookie in JavaScript. Whenever a page loads first
check whether cookie is expired or not. If cookie is not active/expire then first
ask the user name and set it as cookie. If the cookie has not expired then give
appropriate greeting.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible"
content="IE=edge" />
<meta name="viewport" content="width=device-
width, initial-scale=1.0" />
<title>Cookie In Javascript</title>
<script>
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + exdays * 24 * 60 * 60
* 1000);
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";"
+ expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var decodedCookie =
decodeURIComponent(document.cookie);
var ca = decodedCookie.split(";");
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == " ") {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length,
c.length);
}
}
return "";
}
function checkCookie() {
var user = getCookie("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:",
"");
if (user != "" && user != null) {
setCookie("username", user, 30);
}
}
}
</script>
</head>
<body>
<h1>Cookie In Javascript</h1>
<button onclick="checkCookie()">Check
Cookie</button>
</body>
</html>
OUTPUT
document.getElementById("milliseconds").innerHTML =
d.getMilliseconds();
}
</script>
</head>
<body>
<h1>Date Functions</h1>
<button onclick="dateFunctions()">Date
Functions</button>
<p id="date"></p>
<p id="year"></p>
<p id="month"></p>
<p id="date1"></p>
<p id="day"></p>
<p id="hours"></p>
<p id="minutes"></p>
<p id="seconds"></p>
<p id="milliseconds"></p>
</body>
</html>
OUTPUT
CONCLUSION
In conclusion, we explored the fundamentals of form validation, error
management, and cookie handling and put them into practice to build a website.