views:

406

answers:

10

I've got a sign up form that requires the user to enter their email and password, both are in two separate text boxes. I want to provide a button that the user can click so that the password (which is masked) will appear in a popup when the user clicks the button.

currently my JS code for this is as follows:

    function toggleShowPassword() {       
        var button = $get('PASSWORD_TEXTBOX_ID');
        var password;
        if (button)
        {
            password = button.value;
            alert(password);
            button.value = password;  
        } 
   }

The problem is that every time the user clicks the button, the password is cleared in both firefox and IE. I want them to be able to see their password in clear text to verify without having to retype their password.

My questions are:

1) why does the password field keep getting reset with each button click

2) how can I make it so the password field is NOT cleared once the user has seen his/her password in clear text?

+1  A: 

I would assume that the browser has some issue with the script attempting to set the value of a password field:

button.value = password;

This line of code has no real purpose. password.value is not affected in the previous lines where you are reading the value and using it in the alert().

This should be a simpler version of your code:

function toggleShowPassword() {       
    var button = $get('PASSWORD_TEXTBOX_ID');
    if (button)
    {
        alert(button.value);
    }

}

edit: actually I just did a quick test, and Firefox has no problem setting the password field's value with code such as button.value = "blah". So it doesn't seem like this would be the case ... I would check if your ASP.NET code is causing a postback as others have suggested.

matt b
A: 

You do not need to do button.value = password; since reading the value does not change it. I'm not sure why it's being cleared, maybe JavaScript does not allow password field values to be modified.

Rytis
+1  A: 

It sounds that you're doing a request to the server on each click, the password box being reset in each page load is typical behavior of the browsers.

jeds
+1  A: 

You didn't say you were using ASP.NET, but...

By design, ASP.NET clears during postback the value of TextBox controls whose Mode is Password. I work around this in a subclass with the following code:

// If the TextMode is "password", the Text property won't work
if ( TextMode == System.Web.UI.WebControls.TextBoxMode.Password )
    Attributes[ "value" ] = stringValue;
harpo
+1  A: 

If you don't want the button to submit the form, then be sure it has type 'button' rather than 'submit'. For example, you might do something like this:

<input type="button" value="Show My Password" onclick="toggleShowPassword()"/>
pkaeding
+1  A: 

In your HTML:

<input type="button" onclick="toggleShowPassword();">

You need to use "button" rather than "submit" to prevent your form from posting.

JesDaw
+1  A: 

I did a quick example up of a working version:

<html>
    <head>
     <script type="text/javascript" src="prototype.js"></script>
     <script type="text/javascript">
      function toggleShowPassword() {       
       var textBox = $('PasswordText');
       if (textBox)
       {
        alert(textBox.value);  
       } 
      }
     </script>
    </head>
    <body>
     <input type="password" id="PasswordText" /><input type="button" onclick="toggleShowPassword();" value="Show Password" />
    </body>
</html>

The key is that the input is of type button and not submit. I used the prototype library for retrieving the element by ID.

Dale Ragan
A: 

@harpo, sorry I wasn't more clear. I am using asp.net

trying everyone's suggestions now...

eviljack
A: 

argh. nothing works, I think the issues is that I am using asp.net controls for creating users which don't play nicely with javascript. I'm using the CreateUserWizard class for creating users which kind of "hide" the textboxes for password and username inside of the CreateUserWizard control.

eviljack
A: 

hah!

the answer if here: http://forums.asp.net/p/1067527/1548528.aspx

I figured out the solution... the fix was simple change

  OnClientClick="myOnClick()"

to

  OnClientClick="return myOnClick()"

Here's the fully corrected code...

function myOnClick() { //perform some other actions... return false; } Untitled Page

eviljack