Experiment 3
Experiment 3
Aim : Use JavaScript for doing client – side validation of the pages implemented in the experiment
1(shopping cart-app):
1. registration.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.error {
</style>
<script src="../js/registration-validation.js"></script>
</head>
<body>
<header>
<nav>
<ul>
<li><a href="../index.html">Home</a></li>
<li><a href="registration.html">Register</a></li>
<li><a href="login.html">Login</a></li>
<li><a href="catalog.html">Catalog</a></li>
<li><a href="cart.html">Cart</a></li>
</ul>
</nav>
</header>
<div class="registration-container">
<h2>Registration</h2>
<div class="form-group">
</div>
<div class="form-group">
</div>
<div class="form-group">
</div>
<div class="form-group">
</div>
<div class="form-group">
</div>
<label>Gender:</label>
</div>
<div class="form-group">
</div>
<div class="form-group">
</div>
<div class="form-group">
</div>
<div class="form-group">
<label for="state">State:</label>
<option value="Telangana">Telangana</option>
<option value="Andhrapradesh">Andhrapradesh</option>
<option value="Tamilnadu">Tamilnadu</option>
</select>
</div>
<div class="form-group">
<label for="district">District:</label>
<option value="RangaReddy">RangaReddy</option>
<option value="Ananthapur">Ananthapur</option>
<option value="Kanchipuram">Kanchipuram</option>
</select>
</div>
<button type="submit">Register</button>
</form>
</div>
<footer>
</footer>
</body>
</html>
1.2). registration-validation.js:
function validateForm() {
firstname.classList.add("error");
isError = true;
} else {
firstname.classList.remove("error");
}
lastname.classList.add("error");
isError = true;
} else {
lastname.classList.remove("error");
email.classList.add("error");
isError = true;
} else {
email.classList.remove("error");
phone.classList.add("error");
isError = true;
} else {
phone.classList.remove("error");
dob.classList.add("error");
isError = true;
} else {
dob.classList.remove("error");
address.classList.add("error");
isError = true;
} else {
address.classList.remove("error");
isError = true;
} else {
pincode.classList.remove("error");
state.classList.add("error");
isError = true;
} else {
state.classList.remove("error");
district.classList.add("error");
isError = true;
} else {
district.classList.remove("error");
password.classList.add("error");
isError = true;
} else {
password.classList.remove("error");
confirmPassword.classList.add("error");
isError = true;
} else {
confirmPassword.classList.remove("error");
if (gender[i].checked) {
isChecked = true;
break;
if (!isChecked) {
genderError.classList.add("error");
isError = true;
} else {
genderError.classList.remove("error");
if (isError) {
return false;
password.classList.add("error");
confirmPassword.classList.add("error");
return false;
return true;
}
2. login.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
.error {
</style>
<script src="../js/login-validation.js"></script>
</head>
<body>
<header>
<nav>
<ul>
<li><a href="../index.html">Home</a></li>
<li><a href="registration.html">Register</a></li>
<li><a href="login.html">Login</a></li>
<li><a href="catalog.html">Catalog</a></li>
<li><a href="cart.html">Cart</a></li>
</ul>
</nav>
</header>
<div class="login-container">
<h2>Login</h2>
<div class="form-group">
</div>
<div class="form-group">
<label for="password">Password:</label>
</div>
<button type="submit">Login</button>
</form>
</div>
<footer>
</footer>
</body>
</html>
2.1). login-validation.js:
function validateForm() {
username.classList.add("error");
isError = true;
} else {
username.classList.remove("error");
password.classList.add("error");
isError = true;
} else {
password.classList.remove("error");
if (isError) {
return false;
}
return true;
OUTPUT: