0% found this document useful (0 votes)
56 views4 pages

DOM Forms

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views4 pages

DOM Forms

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

<!

DOCTYPE html>
<html>

<head>
<title>DOM Methods</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
background-color: #f0f8ff;
}

h1, h2 {
color: #2e8b57;
text-align: center;
}

h1 {
font-size: 32px;
text-shadow: 2px 2px 4px #aaa;
}

h2 {
font-size: 24px;
margin-top: 20px;
text-decoration: underline;
}

p {
line-height: 1.6;
font-size: 18px;
}

#myParagraph {
color: blue;
font-style: italic;
border: 2px solid #1e90ff;
padding: 10px;
border-radius: 8px;
width: fit-content;
margin: 10px auto;
}

button {
background-color: #4682b4;
color: white;
border: none;
padding: 10px 20px;
margin: 10px;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s ease;
}

button:hover {
background-color: #5a9bd4;
}
button:active {
transform: scale(0.98);
}

/* Animation for newly created elements */


#myParagraph {
animation: fadeIn 1s ease-in-out;
}

@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}

/* Add a shadow to the buttons on hover */


button:hover {
box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.2);
}

form {
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
border: 1px solid #dcdcdc;
width: 300px;
margin: 20px auto;
}

label {
font-size: 16px;
margin-bottom: 8px;
display: block;
}

input[type="text"], input[type="email"] {
width: 100%;
padding: 8px;
margin-bottom: 12px;
border-radius: 4px;
border: 1px solid #ccc;
}

input[type="submit"] {
background-color: #4682b4;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
width: 100%;
font-size: 16px;
transition: background-color 0.3s ease;
}

input[type="submit"]:hover {
background-color: #5a9bd4;
}

input[type="submit"]:active {
transform: scale(0.98);
}

#output {
font-size: 18px;
text-align: center;
color: green;
margin-top: 20px;
}
</style>
</head>

<body>
<h1 id="heading">DOM Forms</h1>
<h2 id="heading">What are DOM forms?</h2>
<p>The DOM provides a programmatic way to interact with, manipulate, and
retrieve data from forms on a web page using JavaScript.</p>
<p>If a form field (fname) is empty, this function alerts a message, and
returns false, to prevent the form from being submitted.</p>

<h2>Form Validation</h2>
<form onsubmit="return validateForm()">
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br><br>

<label for="email">Email:</label>
<input type="email" id="email" name="email"><br><br>

<label for="contactNo">Contact No:</label>


<input type="text" id="contactNo" name="contactNo"><br><br>

<input type="submit" value="Submit">


</form>

<p id="output"></p>

<script>
function validateForm() {
// Get form elements
const name = document.getElementById('name').value;
const email = document.getElementById('email').value;
const contactNo = document.getElementById('contactNo').value;

// Check if fields are empty


if (name === "" || email === "" || contactNo === "") {
alert("All fields must be filled out.");
return false;
}

// Validate email format


const emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
if (!email.match(emailPattern)) {
alert("Please enter a valid email address.");
return false;
}
// Validate contact number (only digits)
const contactPattern = /^[0-9]+$/;
if (!contactNo.match(contactPattern)) {
alert("Contact number must contain only digits.");
return false;
}

// If all validations pass


alert("Form submitted successfully!");
return true;
}
</script>

</body>
</html>

You might also like