Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
sparsh-karna committed Sep 15, 2024
0 parents commit c9d464f
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 0 deletions.
51 changes: 51 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Registration Form</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h2>Registration Form</h2>
<form id="registrationForm">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" id="name" required>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" id="email" required>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" id="password" required>
</div>
<div class="form-group">
<label for="dob">Date of Birth:</label>
<input type="date" id="dob" required>
</div>
<div class="checkbox-group">
<input type="checkbox" id="terms" required>
<label for="terms">Accept Terms & Conditions</label>
</div>
<button type="submit">Submit</button>
</form>
<h3>Entries</h3>
<table id="entriesTable">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Password</th>
<th>Dob</th>
<th>Accepted terms?</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<script src="index.js"></script>
</body>
</html>
55 changes: 55 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
document.addEventListener('DOMContentLoaded', function() {
const form = document.getElementById('registrationForm');
const table = document.getElementById('entriesTable').getElementsByTagName('tbody')[0];

loadTableData();

form.addEventListener('submit', function(e) {
e.preventDefault();

const name = document.getElementById('name').value;
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
const dob = document.getElementById('dob').value;
const terms = document.getElementById('terms').checked;

const birthDate = new Date(dob);
const age = calculateAge(birthDate);
if (age < 18 || age > 55) {
alert('You must be between 18 and 55 years old to register.');
return;
}

const row = table.insertRow();
row.innerHTML = `<td>${name}</td><td>${email}</td><td>${password}</td><td>${dob}</td><td>${terms}</td>`;

saveToLocalStorage({name, email, password, dob, terms});

form.reset();
});
});

function calculateAge(birthDate) {
const today = new Date();
let age = today.getFullYear() - birthDate.getFullYear();
const monthDiff = today.getMonth() - birthDate.getMonth();
if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}

function saveToLocalStorage(data) {
let entries = JSON.parse(localStorage.getItem('formEntries')) || [];
entries.push(data);
localStorage.setItem('formEntries', JSON.stringify(entries));
}

function loadTableData() {
const table = document.getElementById('entriesTable').getElementsByTagName('tbody')[0];
const entries = JSON.parse(localStorage.getItem('formEntries')) || [];
entries.forEach(entry => {
const row = table.insertRow();
row.innerHTML = `<td>${entry.name}</td><td>${entry.email}</td><td>${entry.password}</td><td>${entry.dob}</td><td>${entry.terms}</td>`;
});
}
75 changes: 75 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
.container {
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
width: 100%;
max-width: 500px;
}
h2, h3 {
text-align: center;
}
form {
display: flex;
flex-direction: column;
}
.form-group {
display: flex;
align-items: center;
margin: 10px 0;
}
label {
flex: 1;
margin-right: 10px;
}
input[type="text"],
input[type="email"],
input[type="password"],
input[type="date"] {
flex: 2;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
}
.checkbox-group {
display: flex;
align-items: center;
margin: 10px 0;
}
.checkbox-group input {
margin-right: 5px;
}
button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px;
border-radius: 4px;
cursor: pointer;
margin-top: 10px;
}
button:hover {
background-color: #45a049;
}
table {
width: 100%;
margin-top: 20px;
border-collapse: collapse;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}

0 comments on commit c9d464f

Please sign in to comment.