On form submission, browsers automatically set focus on fields with problematic or missing required values. No JavaScript required!

Screenshot of a sign-in form in Chrome on desktop showing browser prompt and focus for an invalid email value.
Basic built-in validation by the browser.

Validate inline and provide feedback to the user as they enter data, rather than providing a list of errors when they click the submit button. If you need to validate data on your server after form submission, list all problems that are found and clearly highlight all form fields with invalid values, as well as displaying a message inline next to each problematic field explaining what needs to be fixed. Check server logs and analytics data for common errors—you may need to redesign your form.

You should also use JavaScript to do more robust validation while users are entering data and on form submission. Use the Constraint Validation API (which is widely supported) to add custom validation using built-in browser UI to set focus and display prompts.