Toggle navigation
☰
Home
HTML
CSS
Scripting
Database
<!DOCTYPE html> <title>My Example</title> <style> .summary-form-container { max-width: 500px; margin: 40px auto; padding: 30px; background: #fdfdfd; border: 1px solid #e9e9e9; border-radius: 8px; font-family: sans-serif; } /* The error summary box, hidden by default */ .error-summary { display: none; border: 2px solid #dc3545; background-color: #ffebee; color: #c62828; padding: 15px; margin-bottom: 20px; border-radius: 5px; } .error-summary.visible { display: block; } .error-summary h3 { margin: 0 0 10px; font-size: 1.1rem; } .error-summary ul { margin: 0; padding-left: 20px; } .error-summary li a { color: #c62828; font-weight: 500; } /* Form styling */ .form-group { margin-bottom: 15px; } .form-group label { display: block; margin-bottom: 5px; font-weight: 500; } .form-group input { width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 4px; font-size: 1rem; box-sizing: border-box; } /* Highlight invalid fields */ .form-group input.input-error { border-color: #dc3545; background-color: #fff8f8; } .form-group input.input-error:focus { outline: none; box-shadow: 0 0 0 2px rgba(220, 53, 69, 0.25); } .submit-btn { width: 100%; padding: 12px; background: #007bff; color: white; border: none; border-radius: 4px; font-size: 1.1rem; } </style> <div class="summary-form-container"> <form id="summaryForm" action="#" method="post" novalidate> <!-- Error summary box (initially hidden) --> <div class="error-summary" id="errorSummary" role="alert"> <h3>Please correct the following errors:</h3> <ul id="errorList"></ul> </div> <div class="form-group"> <label for="fullName">Full Name</label> <!-- A custom error message can be provided via data-errormsg --> <input type="text" id="fullName" name="fullName" required data-errormsg="Full Name cannot be blank."> </div> <div class="form-group"> <label for="userEmail">Email Address</label> <input type="email" id="userEmail" name="userEmail" required data-errormsg="Please enter a valid email address."> </div> <div class="form-group"> <label for="userAge">Age (must be 18+)</label> <input type="number" id="userAge" name="userAge" required min="18" data-errormsg="You must be at least 18 years old."> </div> <button type="submit" class="submit-btn">Register</button> </form> </div> <script> const summaryForm = document.getElementById('summaryForm'); const errorSummaryBox = document.getElementById('errorSummary'); const errorList = document.getElementById('errorList'); const inputs = Array.from(summaryForm.querySelectorAll('input[required]')); if (summaryForm) { summaryForm.addEventListener('submit', (e) => { // Prevent submission first to run our checks e.preventDefault(); // Reset from previous attempts errorList.innerHTML = ''; errorSummaryBox.classList.remove('visible'); inputs.forEach(input => input.classList.remove('input-error')); const errors = []; // Loop through inputs and check validity inputs.forEach(input => { if (!input.validity.valid) { // If invalid, add to our error list errors.push({ id: input.id, message: input.dataset.errormsg || 'This field is invalid.' }); // And highlight the field input.classList.add('input-error'); } }); // If there are errors, display them if (errors.length > 0) { errors.forEach(error => { const li = document.createElement('li'); const link = document.createElement('a'); link.href = `#${error.id}`; link.textContent = error.message; link.onclick = (event) => { event.preventDefault(); document.getElementById(error.id).focus(); }; li.appendChild(link); errorList.appendChild(li); }); errorSummaryBox.classList.add('visible'); errorSummaryBox.focus(); // Important for accessibility! } else { // If no errors, the form is valid alert('Form is valid and would submit now!'); // In a real application, you would remove the alert and uncomment the line below. // summaryForm.submit(); // Uncomment to allow submission } }); } </script>