0% found this document useful (0 votes)
11 views

IP Codes

Uploaded by

dumacc000.111
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

IP Codes

Uploaded by

dumacc000.111
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

1.

#> Write a JavaScript to check if the password and confirm password are the
same or not.
<!DOCTYPE html>
<html>
<head>
<title>Check Password</title>
<script>
function checkPassword() {
const password = document.getElementById("password").value;
const confirmPassword = document.getElementById("confirmPassword").value;
if (password === confirmPassword) {
alert("Passwords match!");
} else {
alert("Passwords do not match!");
}
}
</script>
</head>
<body>
<form onsubmit="event.preventDefault(); checkPassword();">
<label for="password">Password:</label>
<input type="password" id="password" required><br>
<label for="confirmPassword">Confirm Password:</label>
<input type="password" id="confirmPassword" required><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
2. #> Write a JavaScript that reads ten numbers and displays the count of
negative numbers, the count of positive numbers, and the count of zero from
the list.
<!DOCTYPE html>
<html>
<head>
<title>Count Numbers</title>
<script>
function countNumbers() {
const numbers = prompt("Enter 10 numbers separated by spaces:")
.split(" ").map(Number);
let positive = 0, negative = 0, zero = 0;

numbers.forEach(num => {
if (num > 0) positive++;
else if (num < 0) negative++;
else zero++;
});

alert(`Positive: ${positive}, Negative: ${negative}, Zero: ${zero}`);


}
</script>
</head>
<body>
<button onclick="countNumbers()">Enter Numbers</button>
</body>
</html>
3. #> Write code to process online Alumni information for your college. Create
forms to get name, address, date of birth, and email ID. Validate the
following using JavaScript:
a. All fields are filled before form submission.
b. Email ID contains "@" and ".".
c. Age validation using DOB (>=22 years).
<!DOCTYPE html>
<html>
<head>
<title>Alumni Information</title>
<script>
function validateForm() {
const name = document.getElementById("name").value;
const address = document.getElementById("address").value;
const dob = new Date(document.getElementById("dob").value);
const email = document.getElementById("email").value;

if (!name || !address || !dob || !email) {


alert("All fields must be filled out!");
return false;
}

if (!email.includes("@") || !email.includes(".")) {
alert("Enter a valid email address!");
return false;
}

const age = new Date().getFullYear() - dob.getFullYear();


if (age < 22) {
alert("Age must be 22 or older!");
return false;
}

alert("Form submitted successfully!");


return true;
}
</script>
</head>
<body>
<form onsubmit="return validateForm();">
<label for="name">Name:</label>
<input type="text" id="name" required><br>
<label for="address">Address:</label>
<input type="text" id="address" required><br>
<label for="dob">Date of Birth:</label>
<input type="date" id="dob" required><br>
<label for="email">Email:</label>
<input type="email" id="email" required><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
4. #> Write a JavaScript code to accept a name and password from a user and
validate:
a. Name should not be empty.
b. Password should not be less than 6 characters.
<!DOCTYPE html>
<html>
<head>
<title>Validate Name and Password</title>
<script>
function validateUserInput() {
const name = document.getElementById("name").value;
const password = document.getElementById("password").value;

if (!name) {
alert("Name cannot be empty!");
return false;
}

if (password.length < 6) {
alert("Password must be at least 6 characters long!");
return false;
}

alert("Validation successful!");
return true;
}
</script>
</head>
<body>
<form onsubmit="return validateUserInput();">
<label for="name">Name:</label>
<input type="text" id="name" required><br>
<label for="password">Password:</label>
<input type="password" id="password" required><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
5. #> Create an external stylesheet and link it to an HTML form. The stylesheet
should include the following:
a. A header with red text and a yellow background.
b. A double-lined border table with 5 rows and 3 columns.
c. First column: Sr. No. of the product; second column: image with
hyperlink and product name; third column: description of the product.
<!DOCTYPE html>
<html>
<head>
<title>Styled Form</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>Product Table</header>
<table>
<tr>
<th>Sr. No.</th>
<th>Product</th>
<th>Description</th>
</tr>
<tr>
<td>1</td>
<td><a href="#"><img src="product1.jpg" alt="Product 1" width="50">
Product 1</a></td>
<td>High-quality product</td>
</tr>
<tr>
<td>2</td>
<td><a href="#"><img src="product2.jpg" alt="Product 2" width="50">
Product 2</a></td>
<td>Durable and reliable</td>
</tr>
<tr>
<td>3</td>
<td><a href="#"><img src="product3.jpg" alt="Product 3" width="50">
Product 3</a></td>
<td>Best value for money</td>
</tr>
<tr>
<td>4</td>
<td><a href="#"><img src="product4.jpg" alt="Product 4" width="50">
Product 4</a></td>
<td>Stylish and modern</td>
</tr>
<tr>
<td>5</td>
<td><a href="#"><img src="product5.jpg" alt="Product 5" width="50">
Product 5</a></td>
<td>Eco-friendly product</td>
</tr>
</table>
</body>
</html>

header {
text-align: center;
color: red;
background-color: yellow;
font-size: 24px;
padding: 10px;
}

table {
width: 100%;
border-collapse: collapse;
border: 3px double black;
margin: 20px 0;
}

th, td {
border: 1px solid black;
padding: 10px;
text-align: center;
}

img {
width: 50px;
height: auto;
}
6. #> Write code to drag an image from outside the box and drop it inside the box
using React JS.
import React, { useState } from 'react';

function DragDrop() {
const [dropped, setDropped] = useState(false);

const handleDrop = (e) => {


e.preventDefault();
setDropped(true);
};

const handleDragOver = (e) => {


e.preventDefault();
};

return (
<div>
<h1>Drag and Drop</h1>
<div
onDrop={handleDrop}
onDragOver={handleDragOver}
style={{
width: '300px',
height: '300px',
border: '2px dashed #000',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
}}
>
{dropped ? <img src="image.jpg" alt="Dropped"
style={{ width: '100px' }} /> : 'Drag an image here'}
</div>
<img
src="image.jpg"
alt="Draggable"
draggable
style={{ width: '100px', marginTop: '20px' }}
/>
</div>
);
}

export default DragDrop;

7. #> Write a code in React JS to display "Hello World".


import React from 'react';

function HelloWorld() {
return <h1>Hello World</h1>;
}

export default HelloWorld;


8. #> Write a PHP program to print the factorial of a number.
<!DOCTYPE html>
<html>
<head>
<title>Factorial Program</title>
</head>
<body>
<form method="POST">
<label for="number">Enter a number:</label>
<input type="number" name="number" required>
<button type="submit">Calculate</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = $_POST["number"];
$factorial = 1;
for ($i = 1; $i <= $number; $i++) {
$factorial *= $i;
}
echo "<h3>Factorial of $number is $factorial</h3>";
}
?>
</body>
</html>
9. #> Write a program in JSP to display the system date and time.
<!DOCTYPE html>
<html>
<head>
<title>System Date and Time</title>
</head>
<body>
<h1>Current Date and Time</h1>
<p>
<%
java.util.Date date = new java.util.Date();
out.println(date.toString());
%>
</p>
</body>
</html>
10. #> Create a form with fields 'Name', 'Age', 'Email ID', and 'Password'.
Validate the fields using JavaScript:
a. Name should be between A-Z.
b. Age should be between 0-100.
c. Email ID should contain '@'.
d. Password should have at least 1 uppercase letter, 1 digit, 1 special
character, and a minimum length of 8.
<!DOCTYPE html>
<html>
<head>
<title>Form Validation</title>
<script>
function validateForm() {
const name = document.getElementById('name').value;
const age = document.getElementById('age').value;
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;

const nameRegex = /^[A-Za-z]+$/;


const emailRegex = /@/;
const passwordRegex = /^(?=.*[A-Z])(?=.*\d)(?=.*[@#$%^&+=]).{8,}$/;

if (!nameRegex.test(name)) {
alert('Name should contain only alphabets (A-Z).');
return false;
}

if (age < 0 || age > 100 || isNaN(age)) {


alert('Age should be between 0 and 100.');
return false;
}

if (!emailRegex.test(email)) {
alert('Email ID should contain "@" symbol.');
return false;
}

if (!passwordRegex.test(password)) {
alert('Password must have at least 1 uppercase letter, 1 digit, 1
special character, and be at least 8 characters long.');
return false;
}

alert('Form submitted successfully!');


return true;
}
</script>
</head>
<body>
<form onsubmit="return validateForm()">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br>

<label for="age">Age:</label>
<input type="number" id="age" name="age" required><br>

<label for="email">Email ID:</label>


<input type="email" id="email" name="email" required><br>

<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br>

<button type="submit">Submit</button>
</form>
</body>
</html>
11. #> Write a JSP program to perform basic arithmetic operations: addition,
subtraction, division, and multiplication.
<!DOCTYPE html>
<html>
<head>
<title>Arithmetic Operations</title>
</head>
<body>
<form method="POST">
<label>Enter First Number:</label>
<input type="number" name="num1" required><br>
<label>Enter Second Number:</label>
<input type="number" name="num2" required><br>
<button type="submit">Calculate</button>
</form>
<%
if (request.getMethod().equals("POST")) {
double num1 = Double.parseDouble(request.getParameter("num1"));
double num2 = Double.parseDouble(request.getParameter("num2"));

out.println("<h3>Addition: " + (num1 + num2) + "</h3>");


out.println("<h3>Subtraction: " + (num1 - num2) + "</h3>");
out.println("<h3>Multiplication: " + (num1 * num2) + "</h3>");
if (num2 != 0) {
out.println("<h3>Division: " + (num1 / num2) + "</h3>");
} else {
out.println("<h3>Division: Cannot divide by zero</h3>");
}
}
%>
</body>
</html>
12. #> Write a well-formed XML document to maintain a library catalog with the
name of the book, author, publisher, and year of publishing. Format it in
tabular form using XSLT.
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="library.xsl"?>
<library>
<book>
<name>Book One</name>
<author>Author A</author>
<publisher>Publisher X</publisher>
<year>2021</year>
</book>
<book>
<name>Book Two</name>
<author>Author B</author>
<publisher>Publisher Y</publisher>
<year>2020</year>
</book>
</library>

<?xml version="1.0" encoding="UTF-8"?>


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<head>
<title>Library Catalog</title>
</head>
<body>
<h1>Library Catalog</h1>
<table border="1">
<tr>
<th>Name</th>
<th>Author</th>
<th>Publisher</th>
<th>Year</th>
</tr>
<xsl:for-each select="library/book">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="author"/></td>
<td><xsl:value-of select="publisher"/></td>
<td><xsl:value-of select="year"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
13.
14. #> Write the AJAX code to read from a text file and display its contents
upon button click.
<!DOCTYPE html>
<html>
<head>
<title>AJAX File Reader</title>
<script>
function loadFile() {
const xhr = new XMLHttpRequest();
xhr.open("GET", "file.txt", true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
document.getElementById("content").innerText = xhr.responseText;
}
};
xhr.send();
}
</script>
</head>
<body>
<button onclick="loadFile()">Load File</button>
<div id="content" style="margin-top: 20px; border: 1px solid black; padding:
10px;">
Content will appear here.
</div>
</body>
</html>
15. #> Write a short note and code on JDBC to connect a Java application to a
database.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JDBCExample {


public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/testdb";
String username = "root";
String password = "password";

try {
// Establishing connection
Connection connection = DriverManager.getConnection(
url, username, password);
System.out.println("Connected to the database.");

// Creating a statement
Statement statement = connection.createStatement();

// Executing a query
String query = "SELECT * FROM products";
ResultSet resultSet = statement.executeQuery(query);

// Processing the result set


while (resultSet.next()) {
System.out.println("Product ID: " +
resultSet.getInt("product_id"));
System.out.println("Product Name: " +
resultSet.getString("product_name"));
}
// Closing connection
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
16. #> Write an HTML, XML, and DTD code for an e-commerce website to transfer
the following data:
a. Product ID, Product Name, Product Cost, Purchase Date, Purchased By,
and Seller Name.
<!DOCTYPE html>
<html>
<head>
<title>E-Commerce</title>
</head>
<body>
<h1>E-Commerce Website</h1>
<table border="1">
<tr>
<th>Product ID</th>
<th>Product Name</th>
<th>Product Cost</th>
<th>Purchase Date</th>
<th>Purchased By</th>
<th>Seller Name</th>
</tr>
</table>
</body>
</html>

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE eCommerce SYSTEM "ecommerce.dtd">
<eCommerce>
<product>
<id>101</id>
<name>Smartphone</name>
<cost>15000</cost>
<purchaseDate>2024-01-01</purchaseDate>
<purchasedBy>John Doe</purchasedBy>
<sellerName>ElectroMart</sellerName>
</product>
</eCommerce>

<!ELEMENT eCommerce (product+)>


<!ELEMENT product (id, name, cost, purchaseDate, purchasedBy, sellerName)>
<!ELEMENT id (#PCDATA)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT cost (#PCDATA)>
<!ELEMENT purchaseDate (#PCDATA)>
<!ELEMENT purchasedBy (#PCDATA)>
<!ELEMENT sellerName (#PCDATA)>

17. #> Write a JavaScript code to hide HTML elements.


<!DOCTYPE html>
<html>
<head>
<title>Hide Elements</title>
<script>
function hideElement(id) {
document.getElementById(id).style.display = 'none';
}
</script>
</head>
<body>
<p id="paragraph">This is a paragraph to hide.</p>
<button onclick="hideElement('paragraph')">Hide Paragraph</button>
</body>
</html>
18. #> Write a program to handle form validation in PHP.
<!DOCTYPE html>
<html>
<head>
<title>PHP Form Validation</title>
</head>
<body>
<?php
$name = $email = $age = "";
$errors = [];

if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$errors[] = "Name is required.";
} else {
$name = htmlspecialchars($_POST["name"]);
}

if (empty($_POST["email"])) {
$errors[] = "Email is required.";
} elseif (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {
$errors[] = "Invalid email format.";
} else {
$email = htmlspecialchars($_POST["email"]);
}

if (empty($_POST["age"]) || $_POST["age"] < 0 || $_POST["age"] > 100) {


$errors[] = "Age must be between 0 and 100.";
} else {
$age = htmlspecialchars($_POST["age"]);
}
}
?>
<form method="post">
Name: <input type="text" name="name" value="<?php echo $name; ?>">
<br>
Email: <input type="text" name="email" value="<?php echo $email; ?>">
<br>
Age: <input type="number" name="age" value="<?php echo $age; ?>">
<br>
<button type="submit">Submit</button>
</form>
<?php
if (!empty($errors)) {
echo "<ul>";
foreach ($errors as $error) {
echo "<li>$error</li>";
}
echo "</ul>";
} elseif ($_SERVER["REQUEST_METHOD"] == "POST") {
echo "Form submitted successfully!";
}
?>
</body>
</html>

19. #> Write a JSP program for basic form processing.


<!DOCTYPE html>
<html>
<head>
<title>Form Processing</title>
</head>
<body>
<form method="POST">
Name: <input type="text" name="name" required><br>
Email: <input type="email" name="email" required><br>
<button type="submit">Submit</button>
</form>
<%
if (request.getMethod().equals("POST")) {
String name = request.getParameter("name");
String email = request.getParameter("email");
out.println("<h3>Form Data Submitted:</h3>");
out.println("<p>Name: " + name + "</p>");
out.println("<p>Email: " + email + "</p>");
}
%>
</body>
</html>

You might also like