Project: "Registration Form"
by Andrew Nguyenยท Module 07 - Forms and Validation

Introduction
Not Attempted
NameProgress
Course outline
Not Read
Module 01 - Diving Into React
Not Attempted
NameProgress
Key Concepts
Not Read
Project: Pet Parade
Not Read
Challenge 1
Not Attempted
Quiz 1
Not Attempted
Module 02 - Components and Props
Not Attempted
NameProgress
Project: Profile Card Creator
Not Read
Key Concepts
Not Read
Challenge 2
Not Attempted
Quiz 2
Not Attempted
Module 03 - State and Effect
Not Attempted
NameProgress
Key Concepts
Not Read
Project: Magic Garden
Not Read
Challenge 3
Not Attempted
Quiz 3
Not Attempted
Module 04 - Conditional Rendering
Not Attempted
NameProgress
Key Concepts
Not Read
Project: Weather Wardrobe
Not Read
Challenge 4
Not Attempted
Quiz 4
Not Attempted
Module 05: Handling Events
Not Attempted
NameProgress
Key Concepts
Not Read
Project: "Interactive Quiz App"
Not Read
Challenge 5
Not Attempted
Quiz 5
Not Attempted
Module 06 - Lists and Keys
Not Attempted
NameProgress
Key Concepts
Not Read
Project: "Photo Gallery"
Not Read
Challenge 6
Not Attempted
Quiz 6
Not Attempted
Key Concepts
Project: "Registration Form"
Challenge 7
Quiz 7
Not Attempted
Module 08 - Lifting State Up & Composition vs Inheritance
Not Attempted
NameProgress
Key Concepts
Not Read
Project: "Weather Dashboard"
Not Read
Challenge 8
Not Attempted
Quiz 8
Not Attempted
0 / 470 XP

Project: "Registration Form"

Objective: Create a registration form with validation for each field.

Description: In this project, you will build a registration form that collects user input and validates it before submission. This will help you practice creating controlled components, implementing form validation, and handling form submissions in React.

Instructions:

Step 1: Setup Your Project

  • Create a new React project using Vite.
  • Set up your project structure with a main <strong>App</strong> component and a <strong>RegistrationForm</strong> component.

Step 2: Create Controlled Components

Create state variables to manage the values of the form fields: <strong>username</strong>, <strong>email</strong>, <strong>password</strong>, and <strong>confirmPassword</strong>.

Hint: Use the <strong>useState</strong> hook to create state variables and update them on input changes.

import React, { useState } from 'react';

function RegistrationForm() {
  const [username, setUsername] = useState('');
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');
  const [confirmPassword, setConfirmPassword] = useState('');

  function handleChange(event) {
    const { name, value } = event.target;
    if (name === 'username') setUsername(value);
    if (name === 'email') setEmail(value);
    if (name === 'password') setPassword(value);
    if (name === 'confirmPassword') setConfirmPassword(value);
  }

  return (
    &lt;div&gt;
      &lt;form&gt;
        &lt;label&gt;
          Username:
          &lt;input type="text" name="username" value={username} onChange={handleChange} /&gt;
        &lt;/label&gt;
        &lt;label&gt;
          Email:
          &lt;input type="email" name="email" value={email} onChange={handleChange} /&gt;
        &lt;/label&gt;
        &lt;label&gt;
          Password:
          &lt;input type="password" name="password" value={password} onChange={handleChange} /&gt;
        &lt;/label&gt;
        &lt;label&gt;
          Confirm Password:
          &lt;input type="password" name="confirmPassword" value={confirmPassword} onChange={handleChange} /&gt;
        &lt;/label&gt;
      &lt;/form&gt;
    &lt;/div&gt;
  );
}

export default RegistrationForm;

Step 3: Validate Form Fields

Implement basic validation for the form fields. Ensure that the <strong>username</strong> and <strong>password</strong> fields are not empty, that the <strong>email</strong> field contains a valid email address, and that the <strong>password</strong> and <strong>confirmPassword</strong> fields match.

Hint: Create a function called <strong>validateForm</strong> that checks for validation errors and updates the <strong>errors</strong> state.

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

function validateForm() {
  const newErrors = {};
  if (!username) newErrors.username = 'Username is required.';
  if (!email.includes('@')) newErrors.email = 'Please enter a valid email address.';
  if (!password) newErrors.password = 'Password is required.';
  if (password !== confirmPassword) newErrors.confirmPassword = 'Passwords do not match.';
  return newErrors;
}

Step 4: Handle Form Submission

Create a <strong>handleSubmit</strong> function to handle form submission. This function should prevent the default form submission behavior, validate the form fields, and display appropriate error messages or a success message.

Hint: Use <strong>preventDefault</strong> to stop the form from submitting and check for validation errors before proceeding.

function handleSubmit(event) {
  event.preventDefault();
  const validationErrors = validateForm();
  if (Object.keys(validationErrors).length &gt; 0) {
    setErrors(validationErrors);
  } else {
    setErrors({});
    alert('Registration submitted successfully!');
    // Handle form submission logic here (e.g., send data to server)
  }
}

Step 5: Display Validation Errors

Display validation error messages below each form field if there are validation errors.

Hint: Use conditional rendering to display error messages only when there are errors.

return (
  &lt;div&gt;
    &lt;form onSubmit={handleSubmit}&gt;
      &lt;label&gt;
        Username:
        &lt;input type="text" name="username" value={username} onChange={handleChange} /&gt;
        {errors.username &amp;&amp; &lt;p style={{ color: 'red' }}&gt;{errors.username}&lt;/p&gt;}
      &lt;/label&gt;
      &lt;label&gt;
        Email:
        &lt;input type="email" name="email" value={email} onChange={handleChange} /&gt;
        {errors.email &amp;&amp; &lt;p style={{ color: 'red' }}&gt;{errors.email}&lt;/p&gt;}
      &lt;/label&gt;
      &lt;label&gt;
        Password:
        &lt;input type="password" name="password" value={password} onChange={handleChange} /&gt;
        {errors.password &amp;&amp; &lt;p style={{ color: 'red' }}&gt;{errors.password}&lt;/p&gt;}
      &lt;/label&gt;
      &lt;label&gt;
        Confirm Password:
        &lt;input type="password" name="confirmPassword" value={confirmPassword} onChange={handleChange} /&gt;
        {errors.confirmPassword &amp;&amp; &lt;p style={{ color: 'red' }}&gt;{errors.confirmPassword}&lt;/p&gt;}
      &lt;/label&gt;
      &lt;button type="submit"&gt;Register&lt;/button&gt;
    &lt;/form&gt;
  &lt;/div&gt;
);

Step 6: Style the Registration Form

Add some basic CSS to style your registration form and make it visually appealing.

Hint: Use CSS to style the form elements, error messages, and the submit button.

/* Add to your RegistrationForm.css file */
form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

label {
  display: flex;
  flex-direction: column;
  font-size: 16px;
}

input {
  padding: 8px;
  font-size: 14px;
  border: 1px solid #ccc;
  border-radius: 4px;
}

button {
  background-color: #007bff;
  color: white;
  padding: 10px 20px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}

button:hover {
  background-color: #0056b3;
}

p {
  color: red;
  font-size: 12px;
  margin: 0;
}

Complete Code Example:

Here's how your complete <strong>RegistrationForm</strong> component might look:

 

By completing this project, you'll gain hands-on experience with creating controlled components, validating form data, and handling form submissions in React. Enjoy building your Registration Form and making it robust and user-friendly!

Maggie

Discuss with Maggie
Use the power of generative AI to interact with course content

Discussion

0 comments
Loading editor ...
Remember to be polite and report any undesirable behaviour

Category

Empty

Labels

Discussion has no labels

1 participant

user avatar

Priority

Notifications

You're not receiving notifications from this thread.
Course Outline