Toggle navigation
☰
Home
HTML
CSS
Scripting
Database
<!DOCTYPE html> <title>My Example</title> <style> .validation-form-container { max-width: 450px; margin: 40px auto; padding: 30px; background: #fff; border-radius: 8px; box-shadow: 0 4px 15px rgba(0,0,0,0.1); font-family: sans-serif; } .validation-form-container h2 { text-align: center; } /* -- The core styles -- */ .form-group { margin-bottom: 20px; position: relative; /* Needed for positioning icons */ } .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; } /* Style for error/success icon */ .form-group .status-icon { position: absolute; right: 10px; top: 38px; visibility: hidden; } .form-group .status-icon svg { width: 20px; height: 20px; } /* Style for the helper text */ .form-group small { display: block; margin-top: 5px; font-size: 0.85rem; visibility: hidden; /* Hide by default */ } /* -- Validation State Styles -- */ /* Success State */ .form-group.success input { border-color: #28a745; } .form-group.success .status-icon.success-icon { visibility: visible; color: #28a745; } /* Error State */ .form-group.error input { border-color: #dc3545; } .form-group.error small { visibility: visible; color: #dc3545; } .form-group.error .status-icon.error-icon { visibility: visible; color: #dc3545; } /* -- Submit Button -- */ .submit-btn { width: 100%; padding: 12px; background: #007bff; color: white; border: none; border-radius: 4px; font-size: 1.1rem; cursor: pointer; } </style> <div class="validation-form-container"> <h2>Create Account</h2> <form id="validationForm" action="#" method="post"> <div class="form-group" id="group-username"> <label for="username">Username</label> <input type="text" id="username" name="username" placeholder="Min. 3 characters"> <span class="status-icon success-icon"><svg viewBox="0 0 24 24" fill="currentColor"><path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"></path></svg></span> <span class="status-icon error-icon"><svg viewBox="0 0 24 24" fill="currentColor"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z"></path></svg></span> <small>Error message</small> </div> <div class="form-group" id="group-email"> <label for="email">Email</label> <input type="text" id="email" name="email" placeholder="A valid email address"> <span class="status-icon success-icon"><svg viewBox="0 0 24 24" fill="currentColor"><path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"></path></svg></span> <span class="status-icon error-icon"><svg viewBox="0 0 24 24" fill="currentColor"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z"></path></svg></span> <small>Error message</small> </div> <div class="form-group" id="group-password"> <label for="password">Password</label> <input type="password" id="password" name="password" placeholder="Min. 8 characters"> <span class="status-icon success-icon"><svg viewBox="0 0 24 24" fill="currentColor"><path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"></path></svg></span> <span class="status-icon error-icon"><svg viewBox="0 0 24 24" fill="currentColor"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z"></path></svg></span> <small>Error message</small> </div> <button type="submit" class="submit-btn">Submit</button> </form> </div> <script> const form = document.getElementById('validationForm'); const username = document.getElementById('username'); const email = document.getElementById('email'); const password = document.getElementById('password'); // Utility functions to set error and success states const setError = (input, message) => { const formGroup = input.parentElement; const small = formGroup.querySelector('small'); small.innerText = message; formGroup.className = 'form-group error'; }; const setSuccess = (input) => { const formGroup = input.parentElement; formGroup.className = 'form-group success'; }; // Validation functions const isEmail = (email) => { // Basic regex for email validation return /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(email); }; // Check inputs on blur const checkUsername = () => { const usernameValue = username.value.trim(); if (usernameValue.length < 3) { setError(username, 'Username must be at least 3 characters.'); } else { setSuccess(username); } }; const checkEmail = () => { const emailValue = email.value.trim(); if (!isEmail(emailValue)) { setError(email, 'Email is not valid.'); } else { setSuccess(email); } }; const checkPassword = () => { const passwordValue = password.value.trim(); if (passwordValue.length < 8) { setError(password, 'Password must be at least 8 characters.'); } else { setSuccess(password); } }; if (form) { // Add blur event listeners to each input username.addEventListener('blur', checkUsername); email.addEventListener('blur', checkEmail); password.addEventListener('blur', checkPassword); // Final check on form submit form.addEventListener('submit', (e) => { e.preventDefault(); // Always prevent the default submission first. // Run all checks checkUsername(); checkEmail(); checkPassword(); // Check if all fields are valid before allowing submission const isFormValid = document.querySelectorAll('.form-group.success').length === 3; if(isFormValid) { alert('Form is valid and would submit!'); // In a real application, you would remove the alert and uncomment the line below. // form.submit(); // Uncomment this line to allow actual submission } else { alert('Please fix the errors before submitting.'); } }); } </script>