Lesson 28 of 60 – Form Validation
47%

Form Validation in React

Form validation means checking user-entered data before processing or submitting a form. In React, validation can be performed using state, event handlers, conditions, and validation functions.

Note: Form validation helps ensure that users enter the required and expected information before the form is submitted.

1. What is Form Validation?

Form validation checks whether the information entered by the user meets the required rules.

For example:

  • Name should not be empty.
  • Email should be provided.
  • Password should meet required rules.
  • Age should contain an acceptable value.

2. Why is Form Validation Important?

Validation improves the quality of data collected from users and provides useful feedback when information is missing or incorrect.

  • Prevents incomplete form submission.
  • Helps users correct mistakes.
  • Improves user experience.
  • Reduces invalid data.
  • Checks required fields.

3. Basic Required Field Validation

A simple validation check can determine whether a field is empty.

if (name === "") {

    alert("Name is required");
    return;

}

The form can stop processing when the required value is missing.

4. Validating on Form Submit

Validation can be performed inside the form submission handler.

function handleSubmit(e) {

    e.preventDefault();

    if (name === "") {

        alert("Please enter your name");
        return;

    }

    console.log("Form submitted");

}

5. Using Error State

Instead of displaying an alert, an error message can be stored in React state.

const [error, setError] = useState("");

function handleSubmit(e) {

    e.preventDefault();

    if (name === "") {

        setError("Name is required");
        return;

    }

    setError("");

}

6. Displaying an Error Message

The error state can be displayed conditionally.

{error && (
    <p>
        {error}
    </p>
)}

The message appears when error contains a value.

7. Required Name Validation

const [name, setName] = useState("");
const [error, setError] = useState("");

function handleSubmit(e) {

    e.preventDefault();

    if (name.trim() === "") {

        setError("Name is required");
        return;

    }

    setError("");

}

Using trim() helps detect input containing only spaces.

8. Email Validation

An email field can be checked before the form is submitted.

if (email.trim() === "") {

    setError("Email is required");
    return;

}

Additional checks can be used when a particular email format is required.

9. Basic Email Pattern

A regular expression can be used for a basic email format check.

const emailPattern =
    /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

if (!emailPattern.test(email)) {

    setError("Enter a valid email address");
    return;

}

10. Password Validation

A password can be checked for a minimum length.

if (password.length < 6) {

    setError(
        "Password must be at least 6 characters"
    );

    return;

}

11. Multiple Validation Rules

A form can contain multiple validation rules.

if (name.trim() === "") {

    setError("Name is required");
    return;

}

if (email.trim() === "") {

    setError("Email is required");
    return;

}

if (password.length < 6) {

    setError(
        "Password must be at least 6 characters"
    );

    return;

}

12. Multiple Error Messages

For larger forms, errors can be stored in an object.

const [errors, setErrors] = useState({
    name: "",
    email: "",
    password: ""
});

Each property can contain the validation message for one field.

13. Validation Function

A separate function can keep validation logic organized.

function validateForm() {

    if (name.trim() === "") {

        return "Name is required";

    }

    if (email.trim() === "") {

        return "Email is required";

    }

    return "";

}

14. Using Validation Function on Submit

function handleSubmit(e) {

    e.preventDefault();

    const message = validateForm();

    if (message) {

        setError(message);
        return;

    }

    setError("");

    console.log("Form submitted");

}

15. Required Input Validation

A required field should contain a meaningful value before submission.

<input
    type="text"
    value={name}
    onChange={(e) =>
        setName(e.target.value)
    }
    required
/>

HTML validation can also provide a basic browser-level required check. React applications can additionally implement their own validation logic.

16. Minimum Length Validation

A minimum length rule can be used for fields such as passwords.

if (password.length < 8) {

    setError(
        "Password must contain at least 8 characters"
    );

    return;

}

17. Maximum Length Validation

A maximum length rule can prevent unnecessarily long input.

if (name.length > 50) {

    setError(
        "Name cannot exceed 50 characters"
    );

    return;

}

18. Number Validation

Numeric values can be checked against minimum and maximum limits.

const ageNumber = Number(age);

if (ageNumber < 18) {

    setError(
        "Age must be at least 18"
    );

    return;

}

19. Confirm Password Validation

A confirm password field can be compared with the original password.

if (password !== confirmPassword) {

    setError("Passwords do not match");
    return;

}

20. Showing Error Beside an Input

<input
    type="text"
    value={name}
    onChange={(e) =>
        setName(e.target.value)
    }
/>

{errors.name && (
    <p>
        {errors.name}
    </p>
)}

Displaying the message close to the related field makes the error easier to understand.

21. Validation While Typing

Validation can also be performed while the user types.

function handleNameChange(e) {

    const value = e.target.value;

    setName(value);

    if (value.trim() === "") {

        setError("Name is required");

    } else {

        setError("");

    }

}

22. Validation on Blur

The onBlur event runs when an input loses focus. It can be used to validate a field after the user leaves it.

function validateName() {

    if (name.trim() === "") {

        setError("Name is required");

    } else {

        setError("");

    }

}

<input
    value={name}
    onChange={(e) =>
        setName(e.target.value)
    }
    onBlur={validateName}
/>

23. Validation with Form State Object

const [form, setForm] = useState({
    name: "",
    email: "",
    password: ""
});

function validateForm() {

    const errors = {};

    if (!form.name.trim()) {

        errors.name = "Name is required";

    }

    if (!form.email.trim()) {

        errors.email = "Email is required";

    }

    if (form.password.length < 6) {

        errors.password =
            "Password must contain at least 6 characters";

    }

    return errors;

}

24. Checking Validation Errors

function handleSubmit(e) {

    e.preventDefault();

    const validationErrors = validateForm();

    setErrors(validationErrors);

    if (Object.keys(validationErrors).length > 0) {

        return;

    }

    console.log("Form submitted");

}

Object.keys() can be used to determine whether the errors object contains any validation errors.

25. Complete Registration Validation

import { useState } from "react";

function App() {

    const [form, setForm] = useState({
        name: "",
        email: "",
        password: ""
    });

    const [errors, setErrors] = useState({});

    function handleChange(e) {

        setForm({
            ...form,
            [e.target.name]: e.target.value
        });

    }

    function validateForm() {

        const errors = {};

        if (!form.name.trim()) {

            errors.name = "Name is required";

        }

        if (!form.email.trim()) {

            errors.email = "Email is required";

        }

        if (form.password.length < 6) {

            errors.password =
                "Password must contain at least 6 characters";

        }

        return errors;

    }

    function handleSubmit(e) {

        e.preventDefault();

        const validationErrors = validateForm();

        setErrors(validationErrors);

        if (Object.keys(validationErrors).length === 0) {

            alert("Registration successful");

        }

    }

    return (
        <form onSubmit={handleSubmit}>

            <input
                name="name"
                value={form.name}
                onChange={handleChange}
                placeholder="Name"
            />

            {errors.name && (
                <p>{errors.name}</p>
            )}

            <input
                name="email"
                value={form.email}
                onChange={handleChange}
                placeholder="Email"
            />

            {errors.email && (
                <p>{errors.email}</p>
            )}

            <input
                type="password"
                name="password"
                value={form.password}
                onChange={handleChange}
                placeholder="Password"
            />

            {errors.password && (
                <p>{errors.password}</p>
            )}

            <button type="submit">
                Register
            </button>

        </form>
    );

}

export default App;

26. Validation and Submit Button

A form should not be processed when required validation rules fail.

function handleSubmit(e) {

    e.preventDefault();

    const errors = validateForm();

    if (Object.keys(errors).length > 0) {

        setErrors(errors);
        return;

    }

    submitForm();

}

The actual submission logic runs only after validation succeeds.

27. Clearing Errors

When a user corrects a field, its error message can be cleared.

function handleNameChange(e) {

    const value = e.target.value;

    setForm({
        ...form,
        name: value
    });

    if (value.trim()) {

        setErrors({
            ...errors,
            name: ""
        });

    }

}

28. Client-Side and Server-Side Validation

React can perform client-side validation before sending data to a server.

However, important application data should also be validated on the server because client-side validation alone should not be treated as a security boundary.

React Form
    ↓
Client Validation
    ↓
Server Request
    ↓
Server Validation
    ↓
Process Data

29. Form Validation Workflow

A common React form validation workflow is:

  1. User enters information.
  2. React stores the values in state.
  3. The user submits the form.
  4. React validates the values.
  5. Validation errors are displayed if necessary.
  6. The form stops when validation fails.
  7. Valid data can continue to the next processing step.
User Input
     ↓
React State
     ↓
Submit Form
     ↓
Validate
     ↓
Errors?
  ↙     ↘
Yes      No
 ↓        ↓
Show     Process
Errors   Data

30. React Form Validation Best Practices

  • Validate required fields.
  • Display clear error messages.
  • Use trim() when checking text fields where appropriate.
  • Validate email format when required.
  • Check password rules when required.
  • Compare related fields such as password and confirm password.
  • Keep validation logic organized.
  • Do not submit invalid form data.
  • Validate important data on the server as well.
  • Give users useful feedback so they can correct errors.

📌 Key Points

  • Form validation checks user input before processing it.
  • React state can store validation errors.
  • Validation can happen on submit, change, or blur.
  • Required fields should be checked before submission.
  • Email and password fields can have specific validation rules.
  • Multiple errors can be stored in an object.
  • A validation function helps organize complex validation logic.
  • Error messages should clearly explain what the user needs to correct.
  • Client-side validation improves user experience.
  • Important data should also be validated on the server.

🧠 Quick Quiz

Question: What is the main purpose of form validation in React?