Introduction

In this blog, we will discuss how to create a button to show and hide passwords using jQuery in ASP.NET.

By clicking the checkbox or hovering the mouse on the icon, we can make the password visible.

Step 1

Create a new project in the Visual Studio version of your choice with an empty template. Give it a meaningful name.

Step 2

Add a web form to your project. Right-click and choose a new item, select web form, and give it a name.

Step 3

Add jQuery, bootstrap, and fa icon cdn links to the head section.

  1. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  2. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
  3. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Step 4

Write JavaScript and function to hide and show the password.

  1. <script type="text/javascript">
  2. $(document).ready(function () {
  3. $('#show_password').hover(function show() {
  4. //Change the attribute to text
  5. $('#txtPassword').attr('type', 'text');
  6. $('.icon').removeClass('fa fa-eye-slash').addClass('fa fa-eye');
  7. },
  8. function () {
  9. //Change the attribute back to password
  10. $('#txtPassword').attr('type', 'password');
  11. $('.icon').removeClass('fa fa-eye').addClass('fa fa-eye-slash');
  12. });
  13. //CheckBox Show Password
  14. $('#ShowPassword').click(function () {
  15. $('#Password').attr('type', $(this).is(':checked') ? 'text' : 'password');
  16. });
  17. });
  18. </script>

Step 5

Design HTML by dragging and dropping textbox control, checkbox control, and button control as given below.

  1. <body>
  2. <form id="form1" runat="server">
  3. <div class="container">
  4. <h2>Show or Hide Password</h2>
  5. <div class="row">
  6. <div class="col-md-6">
  7. <p>Hover on the eye to show/hide the password</p>
  8. <label>Password</label>
  9. <div class="input-group">
  10. <asp:TextBox ID="txtPassword" TextMode="Password" runat="server" CssClass="form-control"></asp:TextBox>
  11. <div class="input-group-append">
  12. <button id="show_password" class="btn btn-primary" type="button">
  13. <span class="fa fa-eye-slash icon"></span>
  14. </button>
  15. </div>
  16. </div>
  17. </div>
  18. <div class="col-md-6">
  19. <p>Check to show/hide the password</p>
  20. <label>Password</label>
  21. <div class="input-group">
  22. <asp:TextBox ID="Password" TextMode="Password" runat="server" CssClass="form-control"></asp:TextBox>
  23. <div class="input-group-append">
  24. <span class="input-group-text">
  25. <asp:CheckBox ID="ShowPassword" runat="server" CssClass="checkbox" />
  26. </span>
  27. </div>
  28. </div>
  29. </div>
  30. </div>
  31. </div>
  32. </form>
  33. </body>

Here is the complete code for hiding and showing password.

  1. <!DOCTYPE html>
  2. <html>
  3. <head runat="server">
  4. <title>Show Hide Password</title>
  5. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  6. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
  7. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  8. <script type="text/javascript">
  9. $(document).ready(function () {
  10. $('#show_password').hover(function show() {
  11. //Change the attribute to text
  12. $('#txtPassword').attr('type', 'text');
  13. $('.icon').removeClass('fa fa-eye-slash').addClass('fa fa-eye');
  14. },
  15. function () {
  16. //Change the attribute back to password
  17. $('#txtPassword').attr('type', 'password');
  18. $('.icon').removeClass('fa fa-eye').addClass('fa fa-eye-slash');
  19. });
  20. //CheckBox Show Password
  21. $('#ShowPassword').click(function () {
  22. $('#Password').attr('type', $(this).is(':checked') ? 'text' : 'password');
  23. });
  24. });
  25. </script>
  26. </head>
  27. <body>
  28. <form id="form1" runat="server">
  29. <div class="container">
  30. <h2>Show or Hide Password</h2>
  31. <div class="row">
  32. <div class="col-md-6">
  33. <p>Hover on the eye to show/hide the password</p>
  34. <label>Password</label>
  35. <div class="input-group">
  36. <asp:TextBox ID="txtPassword" TextMode="Password" runat="server" CssClass="form-control"></asp:TextBox>
  37. <div class="input-group-append">
  38. <button id="show_password" class="btn btn-primary" type="button">
  39. <span class="fa fa-eye-slash icon"></span>
  40. </button>
  41. </div>
  42. </div>
  43. </div>
  44. <div class="col-md-6">
  45. <p>Check to show/hide the password</p>
  46. <label>Password</label>
  47. <div class="input-group">
  48. <asp:TextBox ID="Password" TextMode="Password" runat="server" CssClass="form-control"></asp:TextBox>
  49. <div class="input-group-append">
  50. <span class="input-group-text">
  51. <asp:CheckBox ID="ShowPassword" runat="server" CssClass="checkbox" />
  52. </span>
  53. </div>
  54. </div>
  55. </div>
  56. </div>
  57. </div>
  58. </form>
  59. </body>
  60. </html>

Step 5

Run the project by pressing CTRL+F5.
Final Output