Simple Lab
Simple Lab
Write
JavaScript code to validate the format in email, and mobile number in 10 characters, If a textbox has
been left empty, popup an alert indicating when email, mobile number and textbox has been left empty.
<!DOCTYPE html>
<html>
<head>
<title>Form Validation</title>
</head>
<body>
<label>Name:</label>
<label>Email:</label>
<label>Mobile No:</label>
<label>Gender:</label>
<label>Hobbies:</label>
</form>
<script>
function validateForm() {
return;
if (!email.match(emailPattern)) {
return;
if (!mobile.match(mobilePattern)) {
</script>
</body>
</html>
</html>
2. Develop an HTML Form, which accepts any Mathematical expression. Write JavaScript code to
Evaluate the expression and Display the result.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form id="calculator-form">
</form>
<script>
function calculate()
try {
document.getElementById("result").textContent = result;
catch (error)
document.getElementById("result").textContent = "Error";
</script>
</body>
</html>
3. Create a page with dynamic effects. Write the code to include layers and basic animation.
<!DOCTYPE html>
<html>
<head>
<style>
.layer {
position: absolute;
width: 100px;
height: 100px;
background-color: pink;
border-radius: 50%;
</style>
</head>
<body>
<script>
function moveLayer(layer) {
</script>
</body>
</html>
4.write a JavaScript code to find the sum of N natural Numbers. (Use user defined function).
<!DOCTYPE html>
<html>
<head>
<title>Sum of N Natural Numbers</title>
</head>
<body>
<h1>Sum of N Natural Numbers</h1>
<form>
<label for="n">Enter a positive integer (N): </label>
<input type="number" id="n">
<button type="button" onclick="calculateSum()">Calculate Sum</button>
</form>
<p id="result">The sum of the first N natural numbers is: <span id="sum">---
</span></p>
<script>
function calculateSum() {
const n = parseInt(document.getElementById("n").value);
if (n >= 1) {
const sum = sumOfNaturalNumbers(n);
document.getElementById("sum").textContent = sum;
}
else {
document.getElementById("sum").textContent = "Please enter a positive integer (N
>= 1).";
}
}
function sumOfNaturalNumbers(n) {
return (n * (n + 1)) / 2;
}
</script>
</body>
</html>
5. .Write a JavaScript code block using arrays and generate the current date in words, this should include
the day, month and year.
<html>
<body>
<script>
// Arrays to store the names of days and months
const daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday"];
</script>
</body>
</html>
6. .Create a form for Student information. Write JavaScript code to find Total, Average, Result and
Grade.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Student Results</title>
<script>
function calculateResults() {
var marks = document.getElementsByClassName('marks');
var total = 0;
<p id="total"></p>
<p id="average"></p>
<p id="result"></p>
<p id="grade"></p>
</body>
</html>
7. Create a form for Employee information. Write JavaScript code to find DA, HRA, PF, TAX, Gross pay,
Deduction and Net pay.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Employee Payroll Calculation</title>
</head>
<body>
<form id="employeeForm">
<label for="basicPay">Basic Pay: </label><input type="number"
id="basicPayInput">
<button type="button" onclick="calculatePay()">Calculate</button>
</form>
<h2>Payroll Details</h2>
<p id="result"></p>
<script>
function calculatePay() {
const basicPay =
parseFloat(document.getElementById('basicPayInput').value);
// Calculations
const DA = basicPay * (DA_PERCENT / 100);
const HRA = basicPay * (HRA_PERCENT / 100);
const PF = basicPay * (PF_PERCENT / 100);
</body>
</html>
8. Write a program in PHP to change background color based on day of the week using if else if
statements and using arrays .
<!DOCTYPE html>
<html>
<head>
<title>Change Background Color by Day of the Week</title>
<style>
body {
text-align: center;
font-size: 24px;
padding: 50px;
}
</style>
</head>
<body>
<?php
// Get the current day of the week as a number (0 for Sunday, 6 for Saturday)
$dayOfWeek = date("w");
9.Write a simple program in PHP for i) generating Prime number ii) generate Fibonacci series.
<?php
function generatePrimes($maxNumber) {
echo "Prime numbers up to $maxNumber: ";
if ($isPrime) {
echo $number . " ";
}
}
echo "<br>";
}
function fibonacciSeries($numTerms) {
$first = 0;
$second = 1;
echo "<br>";
}
<?php
function removeDuplicates($array) {
$uniqueList = array_unique($array);
<?php
for ($i = 5; $i >= 1; $i--) {
for ($j = 5; $j > $i; $j--) {
echo " ";
}
for($k=1;$k<=$i;$k++)
{
echo "*";
}
echo "<br>";
}
?>
12. Write a simple program in PHP for Searching of data by different criteria.
<!DOCTYPE html>
<html>
<head>
<title>Search Data</title>
</head>
<body>
<h2>Search Data</h2>
<form method="post" action="">
<label>Name:</label>
<input type="text" name="name">
<br><br>
<label>Email:</label>
<input type="text" name="email">
<br><br>
<label>Age:</label>
<input type="text" name="age">
<br><br>
<input type="submit" name="search" value="Search">
</form>
<?php
// Sample data array
$data = [
['name' => 'Guru', 'email' => 'guru@example.com', 'age' => 23],
['name' => 'Prajwal', 'email' => 'prajwal@example.com', 'age' => 25],
['name' => 'virat', 'email' => 'virat@example.com', 'age' => 35],
];
if (isset($_POST['search'])) {
$searchName = $_POST['name'];
$searchEmail = $_POST['email'];
$searchAge = $_POST['age'];
// Perform the search
$results = [];
foreach ($data as $item) {
if (
(empty($searchName) || stripos($item['name'], $searchName) !== false) &&
(empty($searchEmail) || stripos($item['email'], $searchEmail) !== false) &&
(empty($searchAge) || $item['age'] == $searchAge)
) {
$results[] = $item;
}
}
// Display the results
if (!empty($results)) {
echo "<h3>Search Results:</h3>";
echo "<ul>";
foreach ($results as $result) {
echo "<li>Name: {$result['name']}, Email: {$result['email']}, Age:
{$result['age']}</li>";
}
echo "</ul>";
} else {
echo "No results found.";
}
}
?>
</body>
</html>
<?php
session_start();
function generateCaptchaCode($length = 6) {
$characters =
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$captchaCode = '';
return $captchaCode;
}
$captchaCode = generateCaptchaCode();
echo "Captcha Code is: " . $captchaCode;
?>
14 Write a program in PHP to read and write file using form control
First create html program inside htdocs folder with the .html extension
<!DOCTYPE html>
<html>
<head>
<title>File Reader and Writer</title>
</head>
<body>
<form action="read.php" method="post">
<label for="textdata">Enter Text: </label><br>
<textarea name="textdata" id="textdata"></textarea><br>
<input type="submit" value="Write to File"><br><br>
</form>
</body>
</html>
Second you have to create one php program with .php extension
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (!empty($_POST['textdata'])) {
file_put_contents("output.txt", $_POST['textdata']);
echo "Data written to file.<br>";
}
if (!empty($_FILES['filedata']['tmp_name'])) {
$fileContent = file_get_contents($_FILES['filedata']['tmp_name']);
echo "File content: " . htmlspecialchars($fileContent) . "<br>";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>College Name - Home</title>
<style>
body {
font-family: Arial, sans-serif;
}
header {
background-color: #007ACC;
color: #fff;
text-align: center;
padding: 20px;
}
h1 {
margin: 0;
}
nav {
background-color: #333;
color: #fff;
padding: 10px;
}
nav a {
color: #fff;
text-decoration: none;
margin-right: 20px;
}
.content {
padding: 20px;
}
footer {
background-color: #333;
color: #fff;
text-align: center;
padding: 10px;
}
</style>
</head>
<body>
<header>
<h1>College Name</h1>
<p>Empowering the Future</p>
</header>
<nav>
<a href="#">Home</a>
<a href="#">Programs</a>
<a href="#">Admissions</a>
<a href="#">About Us</a>
<a href="#">Contact Us</a>
</nav>
<div class="content">
<h2>Welcome to College Name</h2>
<p>
Welcome to College Name, where we are committed to providing a quality education
and empowering the future.
</p>
</div>
<footer>
© <?php echo date("Y"); ?> College Name
</footer>
</body>
</html>
16.Write a program in PHP for exception handling for i) divide by zero ii) checking date format.
<!DOCTYPE html>
<html>
<head>
<title>Exception Handling</title>
</head>
<body>
<h2>Exception Handling</h2>
<form method="post" action="">
<label>Divide a Number by:</label>
<input type="text" name="divider" placeholder="Enter a number">
<input type="submit" name="divide" value="Divide">
<br><br>
<label>Check Date Format:</label>
<input type="text" name="date" placeholder="YYYY-MM-DD">
<input type="submit" name="check_date" value="Check Date Format">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
try {
if (isset($_POST["divide"])) {
$divider = (int)$_POST["divider"];
if ($divider === 0) {
throw new Exception("Cannot divide by zero.");
}
$result = 10 / $divider;
echo "Result of 10 divided by $divider is $result.";
}
if (isset($_POST["check_date"])) {
$date = $_POST["date"];
if (!preg_match("/^\d{4}-\d{2}-\d{2}$/", $date) || !checkdate(substr($date, 5,
2), substr($date, 8, 2), substr($date, 0, 4))) {
throw new Exception("Invalid date format. Use YYYY-MM-DD format.");
}
echo "Date format is valid: $date";
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
}
?>
</body>
</html>