HTML can describe rules: required, type="email", type="number" with min and max, minlength, maxlength and pattern.
When a form is submitted, the browser checks these rules first. If any fail it shows a message and does not fire submit.
From JavaScript, field.checkValidity() returns true or false, and form.checkValidity() checks every field at once.
field.validity explains the result with flags such as valueMissing, typeMismatch, tooShort, rangeOverflow and patternMismatch.
An invalid field fires an invalid event (which does not bubble; listen with capture on the form). Use it to show your own messages.
novalidate on the form turns off the browser's pop-ups while keeping the validity API, which lets you show inline messages instead.
CSS :invalid and :user-invalid style invalid fields; :user-invalid waits until the person has interacted, so empty required fields are not red on page load.
validationMessage is localised and differs between browsers. Show it, but never compare against its exact text in code or tests; check validity flags instead.
05Definitions
Constraint validation
The browser's built-in checking of rules such as required, type, min, max and pattern.
checkValidity
A method that returns whether a field or form passes its constraints, firing invalid events for failures.
ValidityState
The object in field.validity with a flag for each kind of failure and a valid summary flag.
pattern attribute
A regular expression the whole value must match.
novalidate
A form attribute that turns off automatic validation on submit.
Three exercises: explore and modify, complete the implementation, then build independently. Tests check behaviour, so any correct solution passes.
Make the field required
Let the browser enforce a rule.
const form = document.querySelector("#f");
const name = form.elements.name;
console.log(name.checkValidity());
Explain the problem
Map validity flags to messages.
const qty = document.querySelector("#qty");
function problem(field) {
// TODO: return "Required", "Too small", "Too big" or "OK"
// using field.validity
}
for (const v of ["", "0", "11", "4"]) {
qty.value = v;
console.log(problem(qty));
}
An error summary
Collect every invalid field on submit.
// Write invalidNames(form): returns the names of every invalid control
// (in order). Then set user to "Al", email to "al@example.com",
// age to "9" and log invalidNames(form).join(", ").
09Prediction
Commit to an answer first, then run the real code to compare.
Payment pages mark the card fields with required, inputmode and pattern so the browser catches empty or malformed values before any script runs. JavaScript adds friendlier inline messages using the same validity flags, and the server checks everything again.
invalid does not bubble, so a non-capture listener on the form misses it
invalid is not an event
checkValidity does not fire events
forms cannot listen
5. Why should tests not compare validationMessage text?
It is always empty
It differs between browsers and languages
It is read-only
It contains HTML
6. Which approach is best for a sign-up form?
Only JavaScript checks
HTML constraints plus validity-based messages, and server-side validation
Only HTML constraints
Only server checks
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Make the field required: Prints false; Valid once filled
Explain the problem: Prints each problem; Uses validity
The lesson lives in the companion repository at curriculum/level-03-interactive-web-applications/module-13-forms-and-validation/lesson-063-native-browser-validation/. Every example on this page is generated from those files.
These links open the files in the GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Engineering-Academy). You can also use the ZIP download above.
15Summary and next steps
HTML attributes describe validation rules.
The browser blocks invalid submissions before submit fires.
checkValidity and validity flags expose the result to JavaScript.
Client checks help people; the server must still validate.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationnative form validation
Context: I am learning native form validation in JavaScript.
Task: Explain required, type, min, max, pattern and the validity API with a small form.
Constraints: Beginner-friendly language, short runnable examples, no libraries.
Expected behaviour: I can explain the idea in my own words and predict small programs.
Testing: Finish with three quick questions for me to answer.
Code predictionnative form validation drill
Context: I have practised native form validation.
Task: Give me six fields with values and ask which validity flags are true.
Constraints: Wait for my answer before revealing each result.
Expected behaviour: Each question has exactly one correct output.
Testing: Explain any I miss with a step-by-step trace.
Debuggingnative form validation bug
Context: My invalid listener on the form never runs.
Task: Help me find the cause with questions instead of giving me the fix.
Constraints: Do not rewrite my program.
Expected behaviour: I locate and fix the bug myself.
Testing: Suggest the console.log lines that would reveal the problem.
Exercise generationnative form validation practice
Context: I finished the native form validation exercises.
Task: Create three exercises that read validity flags with exact expected output.
Constraints: Fictional data and exact expected output.
Expected behaviour: Each exercise has tests I can run.
Testing: Put the solutions in a separate hidden section.
Code reviewnative form validation review
Context: Here is my code that uses native form validation.
Task: Review my form markup and suggest built-in constraints that replace custom code.
Constraints: Explain each suggestion briefly and keep behaviour identical.
Expected behaviour: The code is clearer and has no hidden mutation or edge-case bug.
Testing: List the changes in order of importance.