How can I hide the password entered by a user in a dialog prompt in JavaScript? For example, using something like
var passwd = prompt("Enter Password : ", "your password here");
I would like that when e.g. 12345
is entered, it appears like *****
or .....
in the dialog box.
Can anyone suggest how I can do this or provide some example code?
Are you looking for the prompt
function?
var response = prompt("What is your name?"); alert("Hello, " + response);
The dialog will look something like this:
This this probably isn't the best way to get password input, because it does not mask the input. Instead, consider using an HTML form with a password input field.
Maybe you are looking for basic HTTP authentication instead?
You can set this by getting your web server to send a few headers; for example with PHP:
<?php if (!isset($_SERVER['PHP_AUTH_USER'])) { header('WWW-Authenticate: Basic realm="My Realm"'); header('HTTP/1.0 401 Unauthorized'); echo 'Text to send if user hits Cancel button'; exit; } else { echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>"; echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>"; } ?>
This will cause the client to show a dialog like this:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With