java practical 11-16 final gaurav
java practical 11-16 final gaurav
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Change Background Color</title>
</head>
<body>
<h1>Click the Button to Change the Background Color</h1>
<button onclick="changeBackgroundColor()">Change Color</button>
<script>
function changeBackgroundColor() {
// Define an array of colors
const colors = ["#FF5733", "#33FF57", "#3357FF", "#F4FF33", "#FF33A1"];
// Get a random index from the colors array
const randomIndex = Math.floor(Math.random() * colors.length);
// Change the background color
document.body.style.backgroundColor = colors[randomIndex];
}
</script> Output:-
</body>
</html>
<p id="ageMessage"></p>
<p id="countdown"></p>
<script>
// Define an object to store user data
let user = {
name: "John Doe",
age: 0,
birthdayCountdown: 30, // Example countdown value (days)
</body>
</html>
Output:-
<script>
// 1. Basic Function: Greet User
function greetUser() {
const name = document.getElementById("nameInput").value;
if (name) {
document.getElementById("greetingMessage").innerHTML = "Hello, " + name + "!";
} else {
document.getElementById("greetingMessage").innerHTML = "Please enter your name.";
}
}
} Output:-
}
</script>
</body>
</html>
<button type="submit">Submit</button>
</form>
<p id="validationMessage"></p>
<script>
// Create an object to handle validations
// Email validation
if (!formValidator.validateEmail(email)) {
// Password validation
if (!formValidator.validatePassword(password)) {
formValidator.displayMessage("Password must be at least 8 characters long.");
return false; // Prevent form submission
}
// Phone validation
if (!formValidator.validatePhone(phone)) {
formValidator.displayMessage("Please enter a valid 10-digit phone number.");
return false; // Prevent form submission
}
</script> Output:-
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<br><br>
<br><br>
<br><br>
<br><br>
<button type="submit">Submit</button>
<p id="validationMessage"></p>
<script>
// Validation object
let formValidator = {
validateEmail: function(email) {
return emailPattern.test(email);
},
validatePhone: function(phone) {
return phonePattern.test(phone);
},
// Password validation regex (at least 8 characters, must include letters and numbers)
validatePassword: function(password) {
return passwordPattern.test(password);
},
validateZipCode: function(zipCode) {
return zipCodePattern.test(zipCode);
},
displayMessage: function(message) {
document.getElementById("validationMessage").innerHTML = message;
};
function validateForm() {
// Email validation
if (!formValidator.validateEmail(email)) {
return false;
// Phone validation
if (!formValidator.validatePhone(phone)) {
return false;
// Password validation
if (!formValidator.validatePassword(password)) {
return false;
if (!formValidator.validateZipCode(zipCode)) {
return false;
return true;
</script>
</body>
</html>
Output:-
<script>
// 1. DOM Manipulation Functions
// 2. Popover Function
function showPopover(event) {
const popover = document.getElementById("popover");
const button = event.target;
// Position the popover near the button
const buttonRect = button.getBoundingClientRect();
popover.style.left = `${buttonRect.left}px`;
popover.style.top = `${buttonRect.top + buttonRect.height + 5}px`;
popover.style.display = 'block'; // Show the popover
}
</body>
</html>
Output:-