Follow us on Instagram for updates link

Forgot Password Form Using HTML, CSS, and JavaScript

Creating a "Forgot Password" Form Using HTML, CSS, and JavaScript

Published on January 4, 2025

A "Forgot Password" form is a key part of a website’s login process, allowing users to reset their password when they forget it. In this post, we’ll demonstrate how to create a simple "Forgot Password" form using HTML, CSS, and JavaScript.




HTML Code



<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Forgot Password</title>

    <link rel="stylesheet" href="style.css">

</head>

<body>

    <div class="form-container">

        <h2>Forgot Password</h2>

        <form id="forgotPasswordForm">

            <label for="email">Enter your email:</label>

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

            <button type="submit">Reset Password</button>

        </form>

        <div id="responseMessage"></div>

    </div>

    <script src="script.js"></script>

</body>

</html>



  

CSS Code



body {

    font-family: Arial, sans-serif;

    display: flex;

    justify-content: center;

    align-items: center;

    height: 100vh;

    margin: 0;

    background-color: #f4f4f9;

}

.form-container {

    background: #fff;

    padding: 20px;

    border-radius: 10px;

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

    width: 400px;

    text-align: left;

}

h2 {

    color: #333;

    margin-bottom: 20px;

    text-align: center;

}

label {

    display: block;

    margin-bottom: 5px;

    font-weight: bold;

    color: #555;

}

input {

    width: 100%;

    padding: 10px;

    margin-bottom: 15px;

    border: 1px solid #ccc;

    border-radius: 5px;

    font-size: 14px;

}

button {

    background: #28a745;

    color: #fff;

    border: none;

    padding: 10px 20px;

    border-radius: 5px;

    cursor: pointer;

    width: 100%;

    font-size: 16px;

    transition: background 0.3s ease;

}

button:hover {

    background: #218838;

}



  

JavaScript Code



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

    e.preventDefault();

    const email = document.getElementById("email").value;

    // Simple validation for email format

    const emailPattern = /^[^ ]+@[^ ]+\.[a-z]{2,3}$/;

    if (email.match(emailPattern)) {

        document.getElementById("responseMessage").textContent = "Password reset link has been sent to your email!";

        document.getElementById("responseMessage").style.color = "green";

        this.reset();

    } else {

        document.getElementById("responseMessage").textContent = "Please enter a valid email address.";

        document.getElementById("responseMessage").style.color = "red";

    }

});



  

How It Works

1. **HTML**: The form contains an email input field and a submit button, allowing the user to enter the email address linked to their account for password recovery. 2. **CSS**: The form is styled to be user-friendly, clean, and centered for better visual appeal. 3. **JavaScript**: The script validates the email format and, if valid, simulates sending a password reset link. It then displays a success message. If the email format is incorrect, an error message is shown.