HTML5 and CSS3 Lab 3: Build a Styled Sign-Up Form with Built-In Validation, Patterns and Accessible Labels
Goal
Build a sign-up form that the browser validates by itself, with clear labels and styles for valid and invalid fields.
Step 1: signup.html
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Sign Up</title>
<link rel="stylesheet" href="lab3.css">
</head>
<body>
<main>
<h1>Create your account</h1>
<form id="signup" action="/signup" method="post">
<label for="name">Full name</label>
<input id="name" name="name" required minlength="2" maxlength="40" autocomplete="name">
<label for="email">Email</label>
<input id="email" name="email" type="email" required autocomplete="email">
<label for="age">Age (18 to 65)</label>
<input id="age" name="age" type="number" min="18" max="65" step="1" required>
<label for="pin">PIN code (6 digits)</label>
<input id="pin" name="pin" pattern="[0-9]{6}" inputmode="numeric" title="Enter exactly 6 digits" required>
<label for="city">City</label>
<input id="city" name="city" list="cities">
<datalist id="cities"><option value="Mumbai"><option value="Delhi"><option value="Pune"></datalist>
<label><input type="checkbox" name="terms" required> I agree to the terms</label>
<button type="submit">Sign up</button>
</form>
</main>
</body>
</html>
Step 2: signup.css
css
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, sans-serif; background: #f3f6fb; color: #1b2433; }
main { max-width: 28rem; margin: 2rem auto; padding: 1.5rem; background: #fff; border-radius: .75rem; box-shadow: 0 2px 10px rgba(0,0,0,.08); }
label { display: block; margin-top: 1rem; font-weight: 600; }
input:not([type="checkbox"]) { width: 100%; padding: .6rem .7rem; border: 1px solid #8a94a6; border-radius: .4rem; font: inherit; }
input:focus-visible { outline: 3px solid #8ab4ff; outline-offset: 1px; }
input:user-invalid { border-color: #c62828; background: #fff5f5; }
input:user-valid { border-color: #2e7d32; }
input[type="checkbox"] { accent-color: #0b5fff; margin-right: .4rem; }
button { margin-top: 1.5rem; padding: .75rem 1.4rem; border: 0; border-radius: .4rem; background: #0b5fff; color: #fff; font: inherit; font-weight: 600; cursor: pointer; }
button:hover { background: #0846c4; }
What Each Rule Checks
| Field | Rule |
|---|---|
| Full name | required, 2 to 40 characters |
| required, must look like an email | |
| Age | number from 18 to 65 |
| PIN code | exactly six digits, using pattern="[0-9]{6}" |
| City | optional, with suggestions from a datalist |
| Terms | checkbox must be ticked |
How We Tested It
We loaded the form in Chrome and filled it step by step, reading the browser's validity flags.
| Step | Result |
|---|---|
Empty form checkValidity() | false |
Name A | tooShort is true |
Email asha@ | typeMismatch is true |
Age 17 | rangeUnderflow is true |
PIN 12345 | patternMismatch is true |
| All fields valid but terms not ticked | false |
| After ticking terms | true |
Styles for states
:user-invalid and :user-valid color a field only after the person has interacted with it. They are newer than :invalid, so check MDN or Can I Use and keep the form usable without them.
Server side checks This form checks only in the browser. A real site must validate again on the server, because browser checks can be bypassed.
Your Turn
- Add a password field with
minlengthand a visible hint - Show a short message under a field using
aria-describedby - Add
novalidateand write your own checks in JavaScript, then compare the experience
Conclusion
HTML5 validation attributes catch common mistakes without JavaScript. Pair them with labels, clear styles and a server side check.
Quick Summary: Goal; HTML; CSS; rules; tests; exercises.

