Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I hide the password entered via a JavaScript dialog prompt?

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?

like image 787
Prasath Mani Avatar asked Mar 04 '12 12:03

Prasath Mani


1 Answers

Are you looking for the prompt function?

var response = prompt("What is your name?");  alert("Hello, " + response); 

The dialog will look something like this:

enter image description here

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:

enter image description here

like image 193
Dagg Nabbit Avatar answered Oct 16 '22 23:10

Dagg Nabbit