Skip to content
GitHub

Step-by-Step Guide to Validate a Form Using JavaScript

Switch to Zen Mode

Common fields include:

  • Text inputs (e.g., name, username, postal code)
  • Number inputs (e.g., age, quantity)
  • Email inputs
  • Password fields
  • Checkboxes or radio buttons
  • Dropdowns or select boxes :::

In JavaScript, attach a function to the form’s submit event. This function will be responsible for performing the validation before the form is submitted.


Inside your validation function, prevent the form from submitting immediately. This gives you time to check if the inputs are valid. If everything is valid, you can allow the form to submit manually.

  • Use preventDefault(): Prevents the default behavior of the event. For example, prevent a form from submitting.

    form.addEventListener("submit", function(event) {
    event.preventDefault();
    console.log("Form submission prevented.");
    });
    javascript

General syntax for retrieving values from form fields in JavaScript:

// 1. Get the form element (optional, useful for structured access)
const form = document.getElementById("formID");
// 2. Retrieve value using ID
const value = document.getElementById("fieldID").value;
// OR
// 3. Retrieve value using form.elements and field name
const value = form.elements["fieldName"].value;
// OR
// 4. Retrieve value using querySelector
const value = document.querySelector("selector").value;
javascript
  • .value is used to get the current value of input, textarea, or select elements.
  • form.elements["name"] works only if the input has a name attribute.
  • querySelector lets you use any CSS selector (e.g. #id, .class, [name='username']).

Check each field for issues. Examples include:

  • Required fields: Ensure they are not empty.
  • Email format: Check that the input resembles a valid email address.
  • Password match: Confirm two password fields match.
  • Regular expressions: Use regex for complex patterns (e.g., password format, phone numbers, postal codes).
  • Numeric checks: Ensure numeric fields contain only numbers.
  • Date format: Validate date fields against a specific format.
  • Select options: Ensure a valid option is selected from dropdowns.
  • Length checks: Enforce minimum or maximum character limits.
  • Value ranges: For numbers, make sure they fall within an expected range.
  • Checkboxes: Ensure required boxes are checked (e.g., terms & conditions).

If any validation fails, display user-friendly error messages near the respective fields or at the top of the form. This helps users understand what they need to fix.


If any field is invalid, stop the form from submitting and keep the user on the same page. This prevents bad or incomplete data from being sent to the server.


If all validations succeed, either:

  • Allow the form to submit normally, or
  • Submit it programmatically via JavaScript (useful for custom handling or AJAX).

To submit a form programmatically using JavaScript, you can use the .submit() method on the form element.

document.getElementById("formID").submit();
javascript
  • Using .submit() bypasses any attached onsubmit event handlers (like validation). If you need to trigger them, call .submit() only after those checks.
  • If you want to prevent default form submission (e.g., to validate first), use event.preventDefault() in a submit handler.
<form id="myForm" action="/submit-url" method="POST">
<input type="text" id="username" name="username">
<input type="password" id="password" name="password">
</form>
<button onclick="submitForm()">Submit via JS</button>
<script>
function submitForm() {
document.getElementById("myForm").submit();
}
</script>
html

To enhance user experience, you can validate fields as the user types or changes input (e.g., validating email format on blur or password strength on keypress).


Finally, test your form with various valid and invalid inputs to make sure all scenarios are covered and handled gracefully.