-2

Hi I would like to replace the input of my prompt dialog box with asterisks. For example I write tom then i want the alert dialog box to display your password is ***.

<script type="text/JavaScript">
//declared variables
var input1 =0;
var dis = input1.length;


input1=prompt("Please enter your Password here","Enter Password Here");
dis.replace(/./gmi, '*')
window.alert("Valid password "+ input1 + "\n You entered the password " + 
input1);
</script>

I tried this but it said replace is undefined.

Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
Tom
  • 13
  • 5
  • possible duplicate of [How can I hide the password entered via a Javascript dialog prompt?](http://stackoverflow.com/questions/9554987/how-can-i-hide-the-password-entered-via-a-javascript-dialog-prompt) – Alex Apr 02 '15 at 11:50
  • I didn't get any help from that. I need it to be Javascript not html. – Tom Apr 02 '15 at 11:52
  • Why use JS when you can designate the textbox type as password and the entry will be displayed as asteriks. – Dean.DePue Apr 02 '15 at 11:54
  • You shouldn't edit your current question to ask a new one. To ask another question, you should make a new question using the Ask Question button. BTW, make sure your new question is [on-topic](http://stackoverflow.com/help/on-topic) first, your edited in question definitely would not be. – Alexander O'Mara Apr 02 '15 at 21:07

3 Answers3

2

if i properly understand, then you should not use replace(" ", "*") as replace() replaces only first character

use Regular Expressions for that (overload for replace() method). This will look like input1.replace(/./gmi, '*')

Romko
  • 1,808
  • 21
  • 27
1

One could get creative here (it's meant as an alternative to using replace())

var plainText = prompt("Please enter your Password here", "Enter Password Here");
var asterisks = (new Array(plainText.length+1).join("*"));

// if you enter "Tom" you get
//
// plainText = "Tom"
// asterisks = "***"
Alex
  • 23,004
  • 4
  • 39
  • 73
-3

You simply make the entry field like this:

Dean.DePue
  • 1,013
  • 1
  • 21
  • 45