WF
Q1. First Name, last Name and age validation
<html> <head><title>
Registration Form Validation
</title>
</head>
<body>
<h2>Enter Student Details</h2>
<form id="registerForm" onsubmit="return validateForm()">
First Name: <input type="text" id="firstName" placeholder="Enter the First Name">
<br />
<br />
Last Name: <input type="text" id="lastName" placeholder="Enter the Last Name">
<br />
<br />
Age: <input type="number" id="age" placeholder="Enter the age">
<br />
<br />
<button text="Submit"> Submit</button>
</form>
<script>
function validateForm() {
var firstName = document.getElementById("firstName").value;
var lastName = document.getElementById("lastName").value;
var age = document.getElementById("age").value;
var nameRegex = /^[a-zA-Z]+$/;
if (!nameRegex.test(firstName) || !nameRegex.test(lastName)) {
alert('First and Last name should contain only alphabets.');
return false;
}
if (age < 18 || age > 50) {
alert('Age should be between 18 and 50.');
return false;
}
alert('Form is Submitted');
return true;
}
</script>
</body>
</html>
Q2. DAte of birth, Joining Date and Salary Validation
<html> <head><title>
Employee Form Validation
</title>
</head>
<body>
<h2>Employee Registration</h2>
<form id="employeeForm" onsubmit="return validateForm()">
Date of Birth: <input type="date" id="dob" required>
<br />
<br />
Joining Date: <input type="date" id="doj" required>
<br />
<br />
Salary: <input type="number" id="salary" required>
<br />
<br />
<button text="Submit"> Submit</button>
</form>
<script>
function validateForm() {
var DOB = document.getElementById("dob").value;
var DOJ = document.getElementById("doj").value;
var Salary = document.getElementById("salary").value;
var currentDate = new Date();
var inputDob = new Date(DOB);
var inputJoiningDate = new Date(DOJ);
if (inputDob >= currentDate) {
alert('Date of Birth should be in the past.');
return false;
}
if (inputJoiningDate > currentDate) {
alert('Joining Date should be today or in the past.');
return false;
}
if (Salary <= 0) {
alert('Salary should be greater than zero.');
return false;
}
alert('Form is Submitted');
return true;
}
</script>
</body>
</html>
Q3. Email validation
<html> <head><title>
Login
</title>
</head>
<body>
<h2>Login</h2>
<form id="loginForm" onsubmit="return validateForm()">
Email: <input type="text" id="email" required>
<br />
<br />
<button text="Submit"> Submit</button>
</form>
<script>
function validateForm() {
var email = document.getElementById("email").value;
var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) {
alert('Please enter a valid email address.');
return false;
}
alert('Form is Submitted');
return true;
}
</script>
</body>
</html>
Q4. UpperCAse
const msg = "Hello World";
const upper = msg.toUpperCase();
console.log(upper);
Q5.Read 2 files and append the content
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', (req, res) => res.sendFile(__dirname + '/index.html'));
app.post('/appendFiles', (req, res) => {
const [firstFileName, secondFileName] = [req.body.firstFileName, req.body.secondFileName];
fs.readFile(firstFileName, 'utf8', (err, data) => {
if (err) return res.status(500).send('Error reading the first line.');
fs.appendFile(secondFileName, data, (err) => {
if (err) return res.status(500).send('Error appending contents to the second file.');
res.send('Content appended successfully!');
});
});
});
app.listen(3000, () => console.log(Server listening on port 3000));
index.html
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Appender</title>
</head>
<body>
<h2>File Appender</h2>
<form action="/appendFiles" method="post">
<label for="firstFileName">First File Name:</label>
<input type="text" id="firstFileName" name="firstFileName" required>
<br>
<label for="secondFileName">Second File Name:</label>
<input type="text" id="secondFileName" name="secondFileName" required>
<br>
<button type="submit">Append Files</button>
</form>
</body>
</html>
Q6. 404 Error Not Found
const express = require('express');
const fs = require('fs').promises;
const app = express();
app.get('/readFile', async (req, res) => {
const fileName = req.query.fileName;
try {
const content = await fs.readFile(fileName, 'utf8');
res.send(content);
} catch (error) {
res.status(404).send('File not found.');
}
});
app.listen(3000, () => console.log('Server is running at http://localhost:3000'));
(To check the Output -> http://localhost:3000/readFile?fileName=index.html)
Q7. select records from Customer table
npm install mysql2
const mysql = require('mysql2');
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'Sonu@065',
database: 'customers_db',
});
connection.connect((err) => {
if (err) {
console.error('Error connecting to database: ', err);
return;
}
console.log('Connection established');
connection.query('SELECT * FROM customers', (err, results) => {
if (err) {
console.error('Error executing query: ', err);
return;
}
console.log('Records from customers table: ', results);
connection.end((err) => {
if (err) {
console.error('Error closing database: ', err);
return;
}
console.log('Connection closed');
});
})
})
Q8.Upload Field
var express = require("express");
const bodyParser = require("body-parser");
var app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.get("/", function(request, response){
response.writeHead(200,{'content-type':'text/html'});
response.write('<form name="file_input" method="post">');
response.write('<h1> Select file </h1> </br> </br>');
response.write('<input type="file" name="fname"></br></br>');
response.write('<input type="submit"><br>');
response.write('</form>');
response.end();
});
app.post("/", function(req, res){
const filename = req.body.fname;
res.writeHead(200,{'content-type':'text/html'});
res.write("File selected by user is "+filename);
});
app.listen(8080);
console.log("Server is running at http://localhost:8080");