Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I got a code for multi user login page. Everything is OK here but when I provide password, it is showing the text. Usually as a password field it will be masked. Can anyone help me by editing the following code.
Thanks in advance.
Arifin

What I have tried:

HTML
<script language="javascript" type="text/javascript">
<!-- Begin
function LogIn(){
loggedin=false;
username="";
password="";
username=prompt("Username:","");
username=username.toLowerCase();
password=prompt("Password:","");
password=password.toLowerCase();
if (username=="guest" && password=="login") {
loggedin=true;
window.location="home-page.html";
}
if (username=="guest2" && password=="login2") {
loggedin=true;
window.location="home-page2.html";
}
if (loggedin==false) {
alert("Invalid login!");
}
}
// End -->
</script>
<center>
<form><input type=button value="Login!" onClick="LogIn()"></form>
</center>
Posted
Updated 6-Feb-23 22:47pm
v2

1 solution

prompt[^] is a very limited API which instructs the browser to display a message and ask the user to enter some text. There is no way to "mask" the text that is entered.

Rather than using such a clunky script, use a proper login form - use an <input>[^] for both the username and the password, with type="password" for the password input.
HTML
<form>
    <p>
        <label for="username">Username:</label>
        <input type="text" size="20" name="username" id="username">
    </p>
    <p>
        <label for="password">Password:</label>
        <input type="password" size="20" name="password" id="password">
    </p>
</form>
<script>
document.querySelector("form").addEventListener("submit", e => {
    e.preventDefault();
    
    const username = document.getElementById("username").value.toLowerCase();
    const password = document.getElementById("password").value; /* Passwords should be case-sensitive */
    if (username === "guest" && password === "login") {
        location = "home-page.html";
    } else if (username === "guest2" && password === "login2") {
        location = "home-page2.html";
    } else {
        alert("Invalid login!");
    }
});
</script>

NB: This is not even remotely secure. The end user can simply view the source of the page to learn the credentials, or even navigate directly to the "protected" pages. If this is meant to be a real application, and not a learning exercise, then you have a lot of work to do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900