Users make mistakes: they leave fields empty, mistype an email, choose weak passwords. Validation checks the data before it is sent, and it tells the user what to fix.

Why Validate

Form validation checks that the data a user enters is complete and in the correct form. Client-side validation runs in the browser. It is fast, because it needs no trip to the server, and it gives immediate feedback. It can be bypassed, since a user can turn off JavaScript or send a request directly. The server must therefore check the data again. Client-side checks improve the experience, and server-side checks protect the system.

Definition: Form validation. Checking that the data a user enters is complete and in the correct form.

Validating on Submit

A form fires a submit event when the user presses the submit button. A handler for this event can read each field and test it. If a value is wrong, the handler calls event.preventDefault(), which stops the browser from sending the form, and shows a message next to the field. The form is sent only when every check has passed.

Definition: preventDefault. An event method that stops the browser's normal action, such as sending a form.

Required and Length Checks

A required check tests that a field is not empty. The value is first trimmed with trim(), so that a field of spaces counts as empty. A length check compares value.length with a minimum or a maximum, for example at least three characters for a name. HTML also offers attributes such as required, minlength and maxlength that give simple checks without code. They are a useful first layer, and a script adds clearer messages and further rules.

Table 4.2: Simple checks for form fields

Check Code Message
Required value.trim() === "" Name is required
Minimum length value.length < 3 At least 3 characters
Maximum length value.length > 20 At most 20 characters
Number range Number(v) < 18 Age must be 18 or more

Example 4.2: A required name check

<form id="f">
  <input id="name" placeholder="Your name">
  <button type="submit">Send</button>
  <p id="msg" style="color: #C00000"></p>
</form>
<script>
  document.getElementById("f").addEventListener("submit", e => {
    const v = document.getElementById("name").value.trim();
    if (v === "") {
      e.preventDefault();
      document.getElementById("msg").textContent = "Name is required";
    }
  });
</script>

Output in the browser:

If the field is empty or holds only spaces, the message appears and the form stays on the page.

Common mistakes

  • Relying on client-side validation alone and skipping the server check.
  • Not trimming the value, so that a field of spaces passes the required check.
  • Calling preventDefault for every submit, so that valid forms are also blocked.
  • Showing a vague message such as Error instead of saying what to fix.

Key takeaways

  • Validation checks that data is complete and correctly formed before it is sent.
  • Run checks in a submit handler and call preventDefault when a rule fails.
  • Trim values before testing, and compare lengths for size rules.
  • The server must always validate again, because client checks can be bypassed.

Practice questions

  1. Which method stops a form from being sent?
  2. Why must the server validate data again?
  3. Which method removes spaces from both ends of a string?

Answers to the practice questions

  1. event.preventDefault().
  2. A user can bypass client-side checks by disabling JavaScript or sending the request directly.
  3. trim().

Sources

  • Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit IV