Start here.
A form gathers values from a person through controls such as input, select, and textarea. The current text in an input is its value property, and that value is a string even when it looks like a number. The browser's HTML can declare some constraints, such as required fields, but JavaScript still needs to understand and validate the value before using it as a number, date, or other kind of data.
When a form is submitted, the browser emits a submit event on the form. A listener can read controls, decide whether the data is acceptable, and display a result. Calling preventDefault prevents the browser's ordinary submission when the script is handling it. FormData can gather named controls into a convenient structure. A label connected to an input gives the user a readable name and helps assistive technology identify the control.
Input is a boundary because it comes from outside the program's assumptions. An empty value, spaces, an unexpectedly large number, or malformed text can all appear. Validate the meaning you need, then give a useful error near the field when it is invalid. Client-side checks improve the interface, but a server receiving data must also validate it because browser code can be bypassed. Reading a form is therefore about both types and trust.
Build the model.
Forms give inputs names, labels, and submission behavior. Browser constraints such as required and type=email provide useful immediate feedback, but a request can be sent without using the form at all. The server must validate values again before trusting them.
Treat every external value as data with an unknown shape. Trim and validate where appropriate, report errors in language the visitor can act on, and avoid showing raw parser messages. Accessibility is part of correctness here: visible labels and a live error message make a form usable beyond one input method.
How it works.
A label gives an input an understandable name, and a name attribute gives its value a key during submission. FormData reads those named values from a form. The browser can check required, type=email, and length constraints before normal submission, making errors easier for a person to fix. It cannot guarantee server safety because a request can be constructed without this page.
At the server boundary, check that the body is the expected shape, each field has the expected type and length, and values are acceptable for the intended operation. Do not trust a hidden field or a disabled button as permission control. Return a useful error status and message, and show it near the form with accessible feedback. Keep the value as data when displaying it later; avoid turning it into HTML.
Read it line by line.
const form = document.querySelector('form');
form?.addEventListener('submit', event => {
event.preventDefault();
const data = new FormData(form);
const email = String(data.get('email') ?? '').trim();
console.log(email);
});First notice. The form provides a value; it does not prove the value is safe or useful.
The selector looks for a form, and the optional chain registers a submit listener only if one exists. On submission, preventDefault stops the browser's normal submission. FormData reads the form's named controls. data.get('email') may be a string or null; ?? supplies an empty string for absence, String makes the type explicit, and trim removes surrounding whitespace. Displaying it does not prove the address is valid.
See the rule again.
function validateName(value) {
const name = String(value ?? '').trim();
return name.length >= 2;
}
console.log(validateName(' A '));The output is false. The whitespace is removed, leaving one character, which fails the minimum length. The same function would return false for null because ?? supplies an empty string. This client check helps the visitor; the server should repeat the rule before storing or sending the value.
One thing to remember.
Validate at the trust boundary, even when the browser already helped.