Skip to main content

User Registration Form: Purpose, Design, and Code Explanation

 

Why Do We Need to Create Forms?

Forms are essential in web development as they provide a way for users to interact with web applications by submitting data. They are used for various purposes, including:

  1. Collecting User Data: For registration, login, feedback, surveys, and more.
  2. Enabling Interactions: Allowing users to send information to the server.
  3. Dynamic Applications: Forms are key in creating dynamic web applications like e-commerce websites, social media platforms, etc.
  4. Improving User Experience: Forms enable tailored content and services by collecting user preferences or queries.

Code Explanation

HTML

The HTML provides the structure of the form and its elements. Here's a detailed breakdown:

html

..

<div class="form-container">

  • A container that wraps the entire form, styled with CSS for layout and design.

html

..

<h1>Sign Up</h1>

  • A heading that indicates the purpose of the form.

html

..

<form id="signupForm">

  • A form element with an id (signupForm) that allows JavaScript to interact with it.
  • method and action attributes can be added when connecting to a backend.

html

..

<div class="form-group">

  <label for="name">Name</label>

  <input type="text" id="name" placeholder="Enter your name" required>

</div>

  • label: Provides a description for the input. The for attribute associates the label with the input using the input's id.
  • input: A text field for entering the user's name. The placeholder provides a hint to the user, and required ensures the field must be filled before submission.

Other input fields (email and password) follow the same structure with variations for type (email and password) to ensure proper validation.

html

..

<button type="submit">Sign Up</button>

  • A button that submits the form. The type="submit" triggers the form's submit event.

html

..

<p class="error-message" id="error"></p>

  • A paragraph for displaying error messages dynamically.

CSS

The CSS styles the form to make it visually appealing. Key aspects:

css

..

body {

  background: linear-gradient(135deg, #ff9a9e, #fad0c4);

}

  • Creates a gradient background for the page.

css

..

.form-container {

  background: #fff;

  padding: 20px 30px;

  border-radius: 8px;

  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);

}

  • Styles the container with a white background, rounded corners, and shadow for a card-like appearance.

css

..

input {

  width: 100%;

  padding: 10px;

  border: 1px solid #ddd;

  border-radius: 5px;

  transition: border-color 0.3s;

}

input:focus {

  border-color: #ff6b6b;

}

  • The input fields are styled for width and padding. The :focus pseudo-class highlights the field when selected.

css

..

button {

  background-color: #ff6b6b;

  transition: background-color 0.3s;

}

button:hover {

  background-color: #e63946;

}

  • The button changes color on hover for interactivity.

css

..

.error-message {

  color: #e63946;

  display: none;

}

  • Error messages are styled and hidden by default, becoming visible only when needed.

JavaScript

The JavaScript handles form validation and interactivity:

javascript

..

document.getElementById("signupForm").addEventListener("submit", function (e) {

  • Adds an event listener to the form for the submit event. When the form is submitted, the provided function runs.

javascript

..

e.preventDefault();

  • Prevents the default form submission (page reload) to handle the validation using JavaScript.

javascript

..

const name = document.getElementById("name").value.trim();

  • Retrieves and trims the input value to remove unnecessary whitespace.

javascript

..

if (!name || !email || !password) {

  error.textContent = "All fields are required!";

  error.style.display = "block";

}

  • Checks if any field is empty. If so, displays an error message.

javascript

..

alert("Form submitted successfully!");

  • Alerts the user on successful form submission.

 

HTML


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Modern Form Design</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <div class="form-container">
    <h1>Sign Up</h1>
    <form id="signupForm">
      <div class="form-group">
        <label for="name">Name</label>
        <input type="text" id="name" placeholder="Enter your name" required>
      </div>
      <div class="form-group">
        <label for="email">Email</label>
        <input type="email" id="email" placeholder="Enter your email" required>
      </div>
      <div class="form-group">
        <label for="password">Password</label>
        <input type="password" id="password" placeholder="Enter your password" required>
      </div>
      <button type="submit">Sign Up</button>
      <p class="error-message" id="error"></p>
    </form>
  </div>
  <script src="script.js"></script>
</body>
</html>
CSS

body {
  font-family: Arial, sans-serif;
  background: linear-gradient(135deg, #ac8687, #8f6053);
  margin: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.form-container {
  background: #fff;
  padding: 20px 30px;
  border-radius: 8px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  text-align: center;
}

h1 {
  color: #333;
  margin-bottom: 20px;
}

.form-group {
  margin-bottom: 15px;
  text-align: left;
}

label {
  display: block;
  font-size: 14px;
  margin-bottom: 5px;
  color: #555;
}

input {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 14px;
  transition: border-color 0.3s;
}

input:focus {
  border-color: #ff6b6b;
  outline: none;
}

button {
  width: 100%;
  padding: 10px;
  background-color: #ff6b6b;
  color: white;
  border: none;
  border-radius: 5px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

button:hover {
  background-color: #e63946;
}

.error-message {
  color: #e63946;
  font-size: 14px;
  margin-top: 10px;
  display: none;
}



JavaScript

document.getElementById("signupForm").addEventListener("submit", function (e) {
  e.preventDefault();



  const name = document.getElementById("name").value.trim();
  const email = document.getElementById("email").value.trim();
  const password = document.getElementById("password").value.trim();
  const error = document.getElementById("error");

  if (!name || !email || !password) {
    error.textContent = "All fields are required!";
    error.style.display = "block";
  } else if (password.length < 6) {
    error.textContent = "Password must be at least 6 characters!";
    error.style.display = "block";
  } else {
    error.style.display = "none";
    alert("Form submitted successfully!");
    document.getElementById("signupForm").reset();
  }
});


Comments

Popular posts from this blog

CSS Box Model

  CSS Box Model: Comprehensive Guide Overview The CSS Box Model is a foundational concept in web design that governs the layout and spacing of elements on a webpage. It treats every HTML element as a rectangular box, defining how its content, padding, border, and margin interact. Why Do We Need the CSS Box Model? 1.      Precise Layout Control : o     Allows developers to define the exact size and space of elements, including margins, borders, and padding. 2.      Consistency : o     Ensures uniform spacing and layout across different browsers and devices. 3.      Flexibility : o     Adjusts spacing and borders without changing the core content dimensions. Where Do We Need the CSS Box Model? 1.      Website Layout Design : o     Essential for spacing headers, footers, sidebars, and content sections. 2. ...

CSS Units

CSS Units: Comprehensive Notes What Are CSS Units? CSS (Cascading Style Sheets) units are a way to define the size of elements, spacing, and positioning in a web page. These units provide flexibility to design responsive, visually appealing, and functional layouts. CSS units are used in defining properties such as width, height, margin, padding, font size, and more. Example 1: <style>   div {     width: 100px;     height: 50px;   } </style> <div>Box</div> Here, 100px and 50px define the width and height of the div . Example 2: <style>   p {     font-size: 16px;   } </style> <p>This is a paragraph.</p> The font size of the paragraph is set to 16 pixels using CSS units. Purpose of CSS Units CSS units help define scalable and fixed measurements for web elements, ensuring: Consistency in layout design. Adaptability to differ...