Forms: Validation and Required Fields
Solution idea
Native HTML validation should enforce required fields and correct formats before submission. Using type=email, minlength, and pattern rules lets the browser handle most validation without custom JS. The existing JS and CSS rely on these attributes, so changing markup is the key step and should be kept minimal and precise.
Public interview reports
These public, self-reported interview accounts informed this practice version. The exercise is modelled on reported rounds, not copied from them, and the reports are not verified official questions or endorsements.
- Taro: Tesla front end developer interview, 2022
The virtual onsite asked for a simple login form. This item practises form fields with labels, required fields and validation messages.
- Taro: Capital One senior web front end engineer interview, 2023
The Angular-specific round in this account asked to integrate a form so that certain validations occur. This item practises required fields, constraints and validation messages in plain HTML; the reported task used Angular.
Reported in a public Tesla interview account. Tesla frontend interview questions
Reported in a public Capital One interview account. Capital One frontend interview questions
Solution
Overview
Native HTML validation should enforce required fields and correct formats before submission. Using type=email, minlength, and pattern rules lets the browser handle most validation without custom JS. The existing JS and CSS rely on these attributes, so changing markup is the key step and should be kept minimal and precise.
Approach 1 – Attribute-driven validation
Wire labels to inputs with for/id, add required to all fields, set type=email on the email input, and apply minlength and pattern to the password. Keep the provided JS and CSS unchanged so the built-in validation UI is used and tests pass reliably. Ensure each input has a label for accessibility.
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<title>
Signup Form Validation
</title>
</head>
<body>
<h1>
Signup Form
</h1>
<form aria-label='Signup form'>
<label for='name'>
Name
</label>
<input id='name' name='name' type='text' required autocomplete='name' />
<label for='email'>
Email
</label>
<input id='email' name='email' type='email' required autocomplete='email' />
<label for='password'>
Password
</label>
<input
id='password'
name='password'
type='password'
required
minlength='8'
autocomplete='new-password'
pattern='(?=.*[A-Z])(?=.*\d).{8,}'
title='At least 8 characters, including 1 uppercase letter and 1 number.'
/>
<button type='submit'>
Sign up
</button>
<p id='status' aria-live='polite'>
</p>
</form>
<script>
(function () {
const get = (sel) => document.querySelector(sel);
const init = () => {
const form = get('form');
const status = get('#status');
if (!form || !status) return;
form.addEventListener('submit', (e) => {
e.preventDefault();
const valid = form.checkValidity();
if (!valid) {
form.reportValidity();
status.textContent = 'Fix the highlighted fields.';
return;
}
status.textContent = 'Looks good ✅';
});
};
init();
window.addEventListener('DOMContentLoaded', init);
})();
</script>
</body>
</html>body {
font-family: system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
margin: 2rem;
line-height: 1.6;
}
form {
display: flex;
flex-direction: column;
gap: 0.75rem;
max-width: 340px;
}
label {
font-weight: 500;
}
input {
padding: 0.4rem 0.5rem;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
padding: 0.5rem;
background: #007acc;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background: #005fa3;
}
#status {
font-weight: 600;
margin-top: 0.25rem;
}Notes & Pitfalls
Missing for/id pairing makes labels less accessible and breaks click-to-focus behavior. A pattern that is too strict can block valid user input; test it with real examples. Using novalidate disables browser validation APIs like reportValidity().
Use type="email" for built-in email validation. Use minlength + pattern for password policies. Add autocomplete tokens (name/email/new-password) for faster form filling.
Resources
Similar questions
Guides
Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.