• We just launched and are currently in beta. Join us as we build and grow the community.

Show / Hide Password HTML CSS JavaScript with Source Code

JxB1990

Yield Optimizer
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
88
Likes
73
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this comprehensive guide, I will provide you with source codes that demonstrate how to create a show password input field. The source code is composed of HTML, CSS, and JavaScript, making it easy to integrate into your web projects.

What is the Purpose of a Show/Hide Password Feature?

The Show/Hide Password feature is a common functionality in application forms, such as Login and Registration Forms. Password fields are typically masked, meaning the characters entered are replaced with dots or asterisks for security reasons. This feature allows users to toggle the visibility of their entered password, displaying it as plain text, similar to regular input fields. This can be particularly helpful in ensuring that users have typed their passwords correctly, reducing the likelihood of login errors due to mistyped characters.

HTML

The following script is an HTML code that contains the elements of a simple login form. The source code filename is known as index.html

.

  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <title

    >

    Login Form with Password Show Hide </

    title

    >
  5. <link

    href

    =

    "https://fonts.googleapis.com/css2?family=Poppins:wght@300;500;600&display=swap"

    rel

    =

    "stylesheet"

    >
  6. <link

    rel

    =

    "stylesheet"

    href

    =

    "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
  7. integrity=

    "sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
  8. crossorigin=

    "anonymous"

    referrerpolicy=

    "no-referrer"

    /

    >
  9. <link

    rel

    =

    "stylesheet"

    href

    =

    "style.css"

    >
  10. </

    head

    >
  11. <body

    >

  12. <div

    class

    =

    "form-container"

    >
  13. <form

    >
  14. <h3

    >

    Login </

    h3

    >
  15. <label

    for

    =

    "username"

    >

    Username</

    label

    >
  16. <input

    type

    =

    "text"

    name

    =

    "username"

    id

    =

    "username"

    placeholder

    =

    "Enter email or phone"

    >

  17. <label

    for

    =

    "password"

    >

    Password</

    label

    >
  18. <div

    class

    =

    "pass"

    >
  19. <input

    type

    =

    "password"

    class

    =

    "pass"

    name

    =

    "password"

    placeholder

    =

    "Enter assword"

    id

    =

    "password"

    >
  20. <i

    id

    =

    "show-hide"

    class

    =

    "fa-solid fa-eye-slash"

    ></

    i

    >
  21. </

    div

    >
  22. <button

    type

    =

    "submit"

    >

    Log In</

    button

    >
  23. <hr

    >
  24. <div

    class

    =

    "social-container"

    >
  25. <h3

    >

    Login using </

    h3

    >
  26. <div

    class

    =

    "social"

    >
  27. <div

    ><a

    class

    =

    "go"

    href

    =

    "#"

    ><i

    class

    =

    "fab fa-google"

    ></

    i

    >

    Google </

    a

    ></

    div

    >
  28. <div

    ><a

    class

    =

    "fb"

    href

    =

    "#"

    ><i

    class

    =

    "fab fa-facebook"

    ></

    i

    >

    Facebook</

    a

    ></

    div

    >
  29. </

    div

    >
  30. </

    div

    >
  31. </

    form

    >
  32. </

    div

    >

  33. </

    body

    >
  34. <script

    src

    =

    "script.js"

    ></

    script

    >
  35. </

    html

    >

CSS

Here's the CSS (Cascading Stylesheet) file script for the login form. This file script contains the code for the design of the elements in the form. The script is known as style.css

and is loaded in the index file.

  1. *

    {
  2. padding

    :

    0

    ;
  3. margin

    :

    0

    ;
  4. box-sizing

    :

    border-box

    ;
  5. font-family

    :

    'Poppins'

    ,

    sans-serif

    ;
  6. }

  7. body {
  8. background-color

    :

    #a499f6

    ;
  9. }

  10. .form-container

    {
  11. width

    :

    100%

    ;
  12. display

    :

    flex;
  13. padding

    :

    50px

    20px

    ;
  14. justify-items:

    center

    ;
  15. }

  16. form {
  17. display

    :

    block

    ;
  18. margin

    :

    auto

    ;
  19. width

    :

    400px

    ;
  20. background-color

    :

    rgba

    (

    255

    ,

    255

    ,

    255

    ,

    0.13

    )

    ;
  21. border-radius

    :

    10px

    ;
  22. backdrop-filter:

    blur(

    10px

    )

    ;
  23. border

    :

    2px

    solid

    rgba

    (

    255

    ,

    255

    ,

    255

    ,

    0.1

    )

    ;
  24. box-shadow

    :

    0

    0

    40px

    rgba

    (

    8

    ,

    7

    ,

    16

    ,

    0.6

    )

    ;
  25. padding

    :

    50px

    35px

    ;
  26. }

  27. form h3 {
  28. font-size

    :

    25px

    ;
  29. text-align

    :

    center

    ;
  30. color

    :

    #fff

    ;
  31. text-shadow

    :

    2px

    2px

    10px

    black

    ;
  32. }

  33. label {
  34. display

    :

    block

    ;
  35. margin-top

    :

    2rem

    ;
  36. font-size

    :

    1rem

    ;
  37. }

  38. input {
  39. outline

    :

    none

    ;
  40. border

    :

    none

    ;
  41. width

    :

    100%

    ;
  42. background-color

    :

    #f4f3f367

    ;
  43. border-radius

    :

    4px

    ;
  44. padding

    :

    15px

    10px

    ;
  45. margin-top

    :

    8px

    ;
  46. font-weight

    :

    100

    ;
  47. }

  48. input::

    placeholder {
  49. color

    :

    rgb

    (

    87

    ,

    79

    ,

    79

    )

    ;
  50. }

  51. .pass

    {
  52. position

    :

    relative

    ;
  53. }

  54. .pass

    i {
  55. position

    :

    absolute

    ;
  56. top

    :

    50%

    ;
  57. transform

    :

    translateY(

    -50%

    )

    ;
  58. cursor

    :

    pointer

    ;
  59. right

    :

    20px

    ;
  60. }

  61. #show-hide

    {
  62. font-size

    :

    1rem

    ;
  63. }

  64. button

    {
  65. margin-top

    :

    30px

    ;
  66. width

    :

    100%

    ;
  67. background-color

    :

    #ffffff

    ;
  68. padding

    :

    15px

    0

    ;
  69. font-size

    :

    18px

    ;
  70. border-radius

    :

    5px

    ;
  71. cursor

    :

    pointer

    ;
  72. border

    :

    none

    ;
  73. transition

    :

    all 0.1s

    ;
  74. }

  75. button

    :

    hover

    {
  76. transform

    :

    translate

    (

    -0.25rem

    ,

    -0.25rem

    )

    ;
  77. box-shadow

    :

    0.25rem

    0.25rem

    #2a2a2c

    ;
  78. }

  79. hr {
  80. margin-top

    :

    20px

    ;
  81. height

    :

    1px

    ;
  82. background-color

    :

    #ffffff7f

    ;
  83. border

    :

    none

    ;
  84. outline

    :

    navajowhite

    ;
  85. }

  86. .social-container

    h3 {
  87. margin-top

    :

    20px

    ;
  88. margin-bottom

    :

    20px

    ;
  89. }

  90. .social

    {
  91. display

    :

    flex;
  92. justify-content

    :

    center

    ;
  93. width

    :

    100%

    ;
  94. }

  95. .social

    div {
  96. width

    :

    150px

    ;
  97. border-radius

    :

    5px

    ;
  98. padding

    :

    10px

    5px

    ;
  99. background-color

    :

    rgba

    (

    255

    ,

    255

    ,

    255

    ,

    0.27

    )

    ;
  100. text-align

    :

    center

    ;
  101. margin

    :

    0

    14px

    ;
  102. cursor

    :

    pointer

    ;
  103. }

  104. .social

    div:

    hover

    {
  105. background-color

    :

    rgba

    (

    248

    ,

    247

    ,

    247

    ,

    0.511

    )

    ;
  106. }

  107. .social

    a {
  108. text-decoration

    :

    none

    ;
  109. }

  110. .go

    {
  111. color

    :

    #e60000

    ;
  112. }

  113. .fb

    {
  114. color

    :

    blue

    ;
  115. }

  116. .social

    i {
  117. margin-right

    :

    4px

    ;
  118. }

  119. @media

    screen and (

    max-width

    :

    462px

    )

    {
  120. form {
  121. width

    :

    100%

    ;
  122. }
  123. }

JavaScript

Lastly, here's the JS file script containing the code that makes the password field masked and unmasked. The file is known as script.js

and is also loaded in the index file.

  1. const

    showHide =

    document.getElementById

    (

    'show-hide'

    )

    ;
  2. let passwordInput =

    document.getElementById

    (

    'password'

    )

    ;

  3. showHide.addEventListener

    (

    'click'

    ,

    function

    (

    )

    {
  4. showHide.classList

    .toggle

    (

    'show'

    )

    ;

  5. if

    (

    showHide.classList

    .contains

    (

    'show'

    )

    )

    {
  6. showHide.classList

    .remove

    (

    'fa-eye-slash'

    )

    ;
  7. showHide.classList

    .add

    (

    'fa-eye'

    )

    ;
  8. passwordInput.setAttribute

    (

    'type'

    ,

    'text'

    )

    ;
  9. }
  10. else

    {
  11. showHide.classList

    .add

    (

    'fa-eye-slash'

    )

    ;
  12. showHide.classList

    .remove

    (

    'fa-eye'

    )

    ;
  13. passwordInput.setAttribute

    (

    'type'

    ,

    'password'

    )

    ;
  14. }
  15. }

    )

    ;

In the provided JavaScript code snippet:

  • showHide

    gets the element with the id 'show-hide'.
  • passwordInput

    gets the element with the id 'password'.
  • The purpose of the click event listener for showHide

    is to trigger the password field to unmask or mask the entered characters.

Snapshots

Here are some images of the output of the provided source code.

Masked/Hidden Password

Unmasked/Hidden Password

There you have it! I hope this Show/Hide Password Source Code using HTML, CSS, and JavaScript.

Explore more on this website for more Tutorials, Free Source Codes, and Articles covering various programming languages.

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

334,779

334,787

Top