0% found this document useful (0 votes)
28 views5 pages

WebTech Assignment 2

Uploaded by

gautammix
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
28 views5 pages

WebTech Assignment 2

Uploaded by

gautammix
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

Q1)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Browser Detection</title>
</head>
<body>

<h1 id="browser"></h1>

<script>
// Function to detect the browser name
function detectBrowser() {
var browserInfo = navigator.userAgent;
var browserName;

if (browserInfo.includes('Opera') || browserInfo.includes('Opr')) {
browserName = 'Opera';
} else if (browserInfo.includes('Edg')) {
browserName = 'Edge';
} else if (browserInfo.includes('Chrome')) {
browserName = 'Chrome';
} else if (browserInfo.includes('Safari')) {
browserName = 'Safari';
} else if (browserInfo.includes('Firefox')) {
browserName = 'Firefox'
} else {
browserName = 'unknown browser'
}

// Display the detected browser


document.getElementById("browser").innerText = "You are using " + browserName + ".";
}

// Call the function when the page loads


window.onload = detectBrowser;
</script>

</body>
</html>

Q2)
<script>
const form = document.getElementById('registrationForm');
form.addEventListener('submit', function(event) {
event.preventDefault();

const firstName = form.elements['firstName'].value;


const lastName = form.elements['lastName'].value;
const email = form.elements['email'].value;
const password = form.elements['password'].value;
const mobileNumber = form.elements['mobileNumber'].value;
const address = form.elements['address'].value;

const nameRegex = /^[a-zA-Z]+$/;


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

if (!nameRegex.test(firstName) || firstName.length < 6) {


alert("Invalid first name! It should only contain alphabets and have a minimum length of 6
characters.");
return;
}

if (!nameRegex.test(lastName) || lastName.length === 0) {


alert("Invalid last name! It should only contain alphabets and cannot be empty.");
return;
}

if (!emailRegex.test(email)) {
alert("Invalid email address!");
return;
}

if (!passwordRegex.test(password)) {
alert("Invalid password! It should be at least 8 characters long and contain at least one uppercase
letter, one digit, and one special character.");
return;
}

if (!mobileRegex.test(mobileNumber)) {
alert("Invalid mobile number! It should contain 10 digits only.");
return;
}

if (address.trim() === '') {


alert("Address cannot be empty!");
return;
}
alert("Registration successful!");
form.submit();
});
</script>

Q3) (a)
<?php
$a = 20;
$b = 42;

$additionResult = $a + $b;
$subtractionResult = $b - $a;
$multiplicationResult = $a * $b;
$divisionResult = $b / $a;
$modulusResult = $b % $a;
$incrementResult = ++$b;
$decrementResult = --$b;

echo "Addition Operation Result: $additionResult<br>";


echo "Subtraction Operation Result: $subtractionResult<br>";
echo "Multiplication Operation Result: $multiplicationResult<br>";
echo "Division Operation Result: $divisionResult<br>";
echo "Modulus Operation Result: $modulusResult<br>";
echo "Increment Operation Result: $incrementResult<br>";
echo "Decrement Operation Result: $decrementResult<br>";
?>

Q3) (b)
<?php
function isPalindrome($str) {
$cleanedStr = strtolower(str_replace(' ', '', $str));
return $cleanedStr === strrev($cleanedStr);
}

$inputString = "Naman";
if (isPalindrome($inputString)) {
echo "The string '$inputString' is a palindrome.";
} else {
echo "The string '$inputString' is not a palindrome.";
}
?>

Q3) (c)(i)
<?php
// MySQL database created with a table named 'users'
// containing columns 'id', 'name', 'email', and 'phone'

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$servername = "localhost:3307"; // Change to your database server name
$username = "root"; // Change to your database username
$password = ""; // Change to your database password
$dbname = "practice"; // Change to your database name

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Get data from the HTML form


$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];

// Insert data into the 'users' table


$sql = "INSERT INTO users (name, email, phone) VALUES ('$name', '$email', '$phone')";

if ($conn->query($sql) === TRUE) {


echo "Data inserted successfully!";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
}
?>

<!-- HTML form -->


<form method="post" action="">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
Phone: <input type="text" name="phone"><br>
<input type="submit" value="Submit">
</form>

Q3) (c)(ii)
<?php
$servername = "localhost:3307";
$username = "root";
$password = "";
$dbname = "practice";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Retrieve data from the 'users' table


$sql = "SELECT id, name, email, phone FROM users";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
</tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr>
<td>" . $row["id"] . "</td>
<td>" . $row["name"] . "</td>
<td>" . $row["email"] . "</td>
<td>" . $row["phone"] . "</td>
</tr>";
}
echo "</table>";
} else {
echo "No data found.";
}

$conn->close();
?>

You might also like