views:

989

answers:

2

I have a user preference page in which the user can type in their current password, new password and confirm new password. When the user submits the form it sends them to an action. The problem is that when the form submits, the password fields stay filled. I want them to clear. I attempted javascript but when I use document.preferences.submit(), the submit isn't fired, or the action isn't fired, or both. Here is the code:

<script language="JavaScript" type="text/javascript">
    function clearpwd() {
        document.preferences.submit();
        document.preferences.currentPassword.value = '';
        document.preferences.newPassword.value = '';
        document.preferences.confirmPassword.value = '';
    }
</script>
 <% using (Html.BeginForm("ChangePreferences", "Home", FormMethod.Post, new { id="preferences", name="preferences" })) { %>
        <div>
            <div style="text-align:left; width:500px;">
            <div class="FormLabel">User Information:</div>
            <table border="0" cellpadding="4" cellspacing="0">
                <tr>
                    <td>First Name:</td>
                    <td>
                        <%= Html.TextBox("firstName")%>
                        <%= Html.ValidationMessage("FirstName") %>
                    </td>
                </tr>
                <tr>
                    <td>Last Name:</td>
                    <td>
                        <%= Html.TextBox("lastName") %>
                        <%= Html.ValidationMessage("LastName") %>
                    </td>
                </tr>
                <tr>
                    <td>New email:</td>
                    <td>
                        <%= Html.TextBox("email")%>
                        <%= Html.ValidationMessage("Email") %>
                    </td>
                </tr>
                <tr>
                    <td>Billing Method:</td>
                    <td>
                            <%=Html.RadioButton("billingMethod", 'E', new { id = "BillingMethod_E", value = 'E' })%><label for="BillingMethod_E">Electronic</label>

                            <%=Html.RadioButton("billingMethod", 'M', new { id = "BillingMethod_M", value = 'M' })%><label for="BillingMethod_M">Mailing</label>
                    </td>
                </tr>

            </table>
            </div>
            <br /><br />

            <div style="text-align:left; width:500px;">
            <div class="FormLabel">Login and Security Information:</div>
            <table border="0" cellpadding="4" cellspacing="0">
                <tr>
                    <td>Current Password</td>
                    <td>
                        <%= Html.Password("currentPassword") %>
                        <%= Html.ValidationMessage("currentPassword") %>
                    </td>
                </tr>
                <tr>
                    <td>New password:</td>
                    <td>
                        <%= Html.Password("newPassword") %>
                        <%= Html.ValidationMessage("newPassword") %>
                    </td>
                </tr>
                <tr>
                    <td>Confirm new password:</td>
                    <td>
                        <%= Html.Password("confirmPassword") %>
                        <%= Html.ValidationMessage("confirmPassword") %>
                    </td>
                </tr>
                <tr>
                    <td>Security Question:</td>
                    <td>
                        <%= Html.TextBox("securityQuestion") %>
                        <%= Html.ValidationMessage("SecurityQuestion") %>
                    </td>
                </tr>
                <tr>
                    <td>Security Answer:</td>
                    <td>
                        <%= Html.Password("securityAnswer") %>
                        <%= Html.ValidationMessage("SecurityAnswer") %>
                    </td>
                </tr>        
            </table>
            </div>
            <br /><br />
            <table>
                <tr>
                    <td></td>
                    <td><input type="submit" value="Change Preferences" onclick="clearpwd()" /></td>
                </tr>
            </table>
        </div>
    <% } %>
+1  A: 
  • To Debug Javascript Problems, better post the generated HTML
  • To debug Javacript problems easier, use Firefox browser with Firebug Extension.

Edit: You are trying to clear the password, after page is submited. After submission page navigates to another page, so i think, javascript won't affect any dom elements after that.

You can try the following sequence.

  • Store Username and Password on Javascript variables.
  • clear Username and Password values.
  • Post to the page with the stored values.
Palani
A: 

It could be your browser filling in the password field if you have it set to remember your password(s). I've had this happen to me.

Chad Moran