Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I toggle password visibility? [duplicate]

I am trying to create a log-in page and in it, I want there to be a button or check-box that will toggle the visibility of the password. The HTML is:

 function showPassword(){
 var pass = document.getElementById("Password");
    if (pass.type === "password") {
        pass.type = "text";
    }
    else {
        pass.type = "password";
    }
 }
  
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://kit.fontawesome.com/ad6a81734d.js" crossorigin="anonymous"></script>
     <link rel="stylesheet" href="~/css/custom.css"/>
</head>
<h1 id="login_header">Login</h1>

<div class="row">
    <div class="col-md-12">
        <form method="post">
            <div class="text-danger validation-summary-valid" data-valmsg-summary="true"><ul><li style="display:none"></li>
</ul></div>
            <div id ="log_reg" class="form-group">
                <i class="fas fa-envelope" aria-hidden="true"></i>
                <label for="Email">Email</label>
                <input class="form-control" type="email" data-val="true" data-val-email="The Email field is not a valid e-mail address." data-val-required="The Email field is required." id="Email" name="Email" value="">
                <span class="text-danger field-validation-valid" data-valmsg-for="Email" data-valmsg-replace="true"></span>
            </div>
            <div id ="log_reg"class="form-group">
                <i class="fas fa-key" aria-hidden="true"></i>
                <label for="Password">Password</label>
                <input class="form-control" type="password" data-val="true" data-val-required="The Password field is required." id="Password" name="Password">
                <span class="text-danger field-validation-valid" data-valmsg-for="Password" data-valmsg-replace="true"></span>
                <input class="form-control" type="checkbox" data-val="false" id="Show Password" onclick="showPassword()" />
            </div>

                <div class="custom-checkbox">
                    <label for="RememberMe">
                        <input class="form-control" type="checkbox" data-val="true" data-val-required="The Remember me field is required." id="RememberMe" name="RememberMe" value="true">
                        Remember me
                    </label>
                </div>

            <button id="button1" type="submit" class="btn btn-primary">Login</button>
        <input name="__RequestVerificationToken" type="hidden" value="CfDJ8OZRytqGjWNEjmm5lNWbbA4QbTKNuzp2dAWmUxUXfDlU4MdRo0VC9BJ7Irjjcx-3bsw-uBfYK-9723M-E4t0uFUUXglXGEGD7bAuWsxnMzHhX0hwOTX1p-bejSkH4ONUDjfhMOyBcMNpkmty_dYln1M"><input name="RememberMe" type="hidden" value="false"></form>
    </div>
</div>

I have no idea what I am doing wrong here, but when I go to check the checkbox the password field remains the same and doesn't reveal itself. I have tried putting a script tag in the HTML file, but it doesn't do anything.

like image 661
Sirrah025 Avatar asked Dec 05 '19 03:12

Sirrah025


3 Answers

There is no 'toggle element' function that I know of, so I do it this way.

<!DOCTYPE html>
<html>
<body>


Password: <input type="password" value="password" id="myInput"><br><br>
<input type="checkbox" onclick="myFunction()">Show the Password

<script>
function myFunction() {
  var pw_ele = document.getElementById("myInput");
  if (pw_ele.type === "password") {
    pw_ele.type = "text";
  } else {
    pw_ele.type = "password";
  }
}
</script>

</body>
</html>
like image 60
j D3V Avatar answered Oct 06 '22 00:10

j D3V


You can simply do like this

function togglePassword(){
var type = document.getElementById("Password").type;
if(type=='password'){
 document.getElementById("Password").type = "text";
 }else{
  document.getElementById("Password").type = "password";
 }
}
<input type="password" id="Password" name="Password">
 
<input class="form-control" type="checkbox" onclick="togglePassword()" />
like image 36
Sourav Singh Avatar answered Oct 05 '22 23:10

Sourav Singh


function showPassword() {
  const password = document.querySelector('#Password');
  password.type = (password.type==="password") ? "text" : "password";
  password.focus();
}
<form method="post">
    <div class="text-danger validation-summary-valid" data-valmsg-summary="true">
        <ul>
            <li style="display:none"></li>
        </ul>
    </div>
    <div id="log_reg" class="form-group">
        <i class="fas fa-envelope" aria-hidden="true"></i>
        <label for="Email">Email</label>
        <input class="form-control" type="email" data-val="true" data-val-email="The Email field is not a valid e-mail address." data-val-required="The Email field is required." id="Email" name="Email" value="">
        <span class="text-danger field-validation-valid" data-valmsg-for="Email" data-valmsg-replace="true"></span>
    </div>
    <div id="log_reg" class="form-group">
        <i class="fas fa-key" aria-hidden="true"></i>
        <label for="Password">Password</label>
        <input class="form-control" type="password" data-val="true" data-val-required="The Password field is required." id="Password" name="Password">
        <span class="text-danger field-validation-valid" data-valmsg-for="Password" data-valmsg-replace="true"></span>
        <input class="form-control" type="checkbox" data-val="false" id="Show Password" onclick="showPassword()" />
    </div>

    <div class="custom-checkbox">
        <label for="RememberMe">
            <input class="form-control" type="checkbox" data-val="true" data-val-required="The Remember me field is required." id="RememberMe" name="RememberMe" value="true"> Remember me
        </label>
    </div>

    <button id="button1" type="submit" class="btn btn-primary">Login</button>
    <input name="__RequestVerificationToken" type="hidden" value="CfDJ8OZRytqGjWNEjmm5lNWbbA4QbTKNuzp2dAWmUxUXfDlU4MdRo0VC9BJ7Irjjcx-3bsw-uBfYK-9723M-E4t0uFUUXglXGEGD7bAuWsxnMzHhX0hwOTX1p-bejSkH4ONUDjfhMOyBcMNpkmty_dYln1M">
    <input name="RememberMe" type="hidden" value="false">
</form>
like image 30
Joel Garcia Nuño Avatar answered Oct 06 '22 00:10

Joel Garcia Nuño