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?
There is currently no way to edit the
prompt()
function in JavaScript to make it hide the text input.Instead, we need to create an popup in HTML and show it when needed. I've created a minimalist example here:
Most likely you want it to look like a popup, so I added some basic CSS to do so here:
Altogether, you get this demo
I have tried to solve the same problem in the following way:
In the main page the user shall press a button in order to launch the request pressing a button will open a popup windows by the JavaScript command
window.open("password.html","passwordRequest",windowStyleVar)
, wherewindowStyleVar
may be:password.html looks like:
The
window.opener.processRequest()
call is very important as it returns the control to the main page forcing main page javascript to complete the intended action.A solution to me, builded up with the help of the above and the tutorial from http://bluebirdjs.com/docs/async-dialogs.html was to use an async function. Because I wanted to replace the prompt and its logic. Unfortunately i could not find an easier solution, but this worked for me:
now with this you can use the await inside an async-function to get nearly the same result as the window.prompt():
you see that this code nearly looks like the use of the old "prompt". The big difference is that we now use async-code - so the code does not stop executing while waiting for the password - just your async-function will wait till the password is either entered or . but for me it helped a lot to not rewrite my whole code anew just to handle the callback-function and stuff. note that this only works in browsers which supports async, so from 2017 onward. old browsers like on older ipads or older versions of ios in general it will not work. if you want to check for the event the user did not enter anything use
You should use an input element with type
password
in a form element:Are you looking for the
prompt
function?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:
This will cause the client to show a dialog like this:
As of many answers you can't mask the input with a JavaScript
prompt()
but some alternative way to done this functionality using custom solution or use password input box instead.Use a modal dialog to require that the user enter data during a multi-step process. Embed form markup in the content area, set the modal option to true, and specify primary and secondary user actions with the buttons option.
Ref : Dialog Modal Form
Using
html
option you write html tags.Ref : Impromptu Plugin
Only by opening a little window containing a password form field:
Ref : irt.org