Skip to content
Tutorials Center
My learning

Topics

Lesson 54 of 58 ยท Projects

Project: Login and Signup Forms

Build login and signup forms in Bootstrap 5 with floating labels, validation messages, a password show/hide button and a switch between the two forms. Nothing is sent.

Almost every application starts with a login screen, and almost every login screen is a small form with big usability demands. People should see what is wrong without guessing, show their password if they are typing on a phone, and move between "log in" and "create account" without leaving the page. Bootstrap gives you the pieces: floating labels, validation styles, input groups and nav pills. A few lines of JavaScript tie them together.

This project is purely front end. The forms never send anything anywhere: the submit handler prints what would have been sent. A real login needs a server, which checks the password against a stored hash, sets a secure session cookie and protects against guessing attacks. The browser-side checks here are a convenience for the user, not security.

You should know Form Controls, Input Groups and Floating Labels and Form Layout and Validation before starting.

Design decisions

Good authentication forms follow a few simple rules:

  • Use a real <label> for every field. Floating labels qualify, because the label element is still there.
  • Set the correct type and autocomplete values (email, current-password, new-password). Password managers rely on them.
  • Say what is wrong next to the field (invalid-feedback), not in a pop-up.
  • Never block pasting into password fields.
  • Offer a show/hide toggle for the password.
flowchart TD
  A["User opens page"] --> B{"Which form?"}
  B -->|"Has an account"| L["Login form"]
  B -->|"New"| S["Signup form"]
  L --> V1{"Valid?"}
  S --> V2{"Valid and<br/>passwords match?"}
  V1 -->|"No"| E1["Show invalid-feedback"]
  V2 -->|"No"| E2["Show invalid-feedback"]
  V1 -->|"Yes"| OK["Show result message<br/>nothing is posted"]
  V2 -->|"Yes"| OK

Both forms validate in the browser first and only then show the result.

Step 1: the login form

A floating label sits inside the input and moves up when the field has focus or text. The markup is form-floating around an input followed by a label. The input needs a placeholder attribute (even a single space or text) because the floating effect relies on the :placeholder-shown state.

For the password toggle, use an input-group with a button after the input. A tiny script switches the input type between password and text, updates the button text, and keeps aria-pressed correct. The button has type="button", otherwise it would submit the form.

The submit handler follows the pattern from the forms lessons: preventDefault(), add was-validated, and if valid print a message instead of sending data. Printing the password would be wrong even in a demo, so only the email is shown.

Login form with password toggle

Try pressing the button with an empty form, then with a short password. Notice has-validation on the input group: it tells Bootstrap to round the corners correctly when the feedback message is inside the group.

Browsers autofill and password managers. A page in this lesson's sandbox cannot save passwords, but on your own site the autocomplete values above are what let password managers fill the right data. Do not use autocomplete="off" on login fields.

Step 2: signup and switching forms

The signup form adds a name, a confirmation password and a terms checkbox. Password matching is not built into HTML validation, but there is a standard hook: setCustomValidity(message). A non-empty message marks the field invalid and makes checkValidity() return false; an empty string clears the error. Bootstrap's :invalid styling then does the rest, and the invalid-feedback text can mention both problems.

To switch between the forms use two nav pills and the tab plugin (see Navs and Tabs). This needs no switching code at all: each pill has data-bs-toggle="pill" and points at its pane. The page does not reload, and each form keeps what the user typed when they go back and forth.

Login and signup with tabs

Submit the signup form with different passwords and watch the message change. The result box hides itself every time the visitor switches tabs, using the shown.bs.tab event (see Plugin Events and Programmatic Control).

Improving the project

When you take this beyond a demo, work through the list:

  1. Server validation. Repeat every check on the server. The browser's checks protect only honest users.
  2. Safe errors. For logins, say "email or password is incorrect" rather than which of the two was wrong.
  3. Password strength. Show a hint or a progress bar for length and variety; encourage long passphrases rather than complicated rules.
  4. Accessibility. Use role="status" or aria-live for the result, keep visible focus outlines, and test the whole flow with the keyboard alone (see Accessibility in Bootstrap).
  5. Rate limiting and CSRF. These live on the server and cannot be solved with CSS or HTML.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which attribute is required on an input inside form-floating for the floating effect to work?

    The floating label uses the :placeholder-shown state, so the input needs a placeholder attribute, even a short one.
  2. 2. How can you mark a confirm-password field invalid when it differs from the password?

    A non-empty custom validity message makes the field invalid; an empty string makes it valid again.
  3. 3. Why does the password toggle button need type="button"?

    The default type of a button is submit, which would send the form (and trigger validation) every time it is pressed.
  4. 4. What does the submit handler call first to stop the browser from posting the form?

    preventDefault() cancels the browser's own form submission so your code can decide what happens.

Summary

  • Floating labels need an input with a placeholder, followed by a label inside form-floating.
  • A show/hide password button is an input-group button with type="button" that switches the input type and keeps aria-pressed in sync.
  • Use novalidate, was-validated and invalid-feedback for messages next to each field; setCustomValidity handles rules like matching passwords.
  • Nav pills with data-bs-toggle="pill" switch between the login and signup forms with no extra script.
  • Browser validation is for convenience; real security (hashing, sessions, rate limits) belongs on the server.

Next lesson: Project: Portfolio Site.