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 Complete Notes

Module 1: Introduction to CSS What is CSS? CSS (Cascading Style Sheets) is a style sheet language used for describing the presentation of a document written in HTML or XML. It controls the layout and appearance of web pages, making them visually appealing and user-friendly. Example: <!DOCTYPE html> <html> <head>     <style>         body {             background-color: lightblue;         }     </style> </head> <body>     <h1>Welcome to CSS</h1>     <p>CSS styles this page!</p> </body> </html> Purpose of CSS Separation of Content and Design: Keeps HTML focused on structure while CSS handles design. Consistency: Ensures uniform styling across multiple web pages. Improved Accessibility: Makes we...

What Is SCADA System

SCADA                 Supervisory Control and Data Acquisition or SCADA is an automation control system used in industries such as energy, oil and gas, water power and many more.         The system has a centralized system that monitors and controls the entire country from industrial plants to plant premises.             The SCADA system works by working with signals that communicate through channels to provide the user with remote control of any device in a system.        There are some essential parts composed of highly controllers and monitoring Systems. 1. SCADA system human machine interface or HMI supervisory system remote terminal units 2. RTu  use programmable logic controllers or PLC and communication infrastructure. HMI processes the data and sends it to the human operator. Where they can monitor or control the system.          The supervi...