[RESOLVED]javascript should work after Regular Expression Validatior

Hi all,

        I have a form named ChangePassword.aspx. Here I am having 3 text boxes, 1st one is for old password, 2nd is for new password and the 3rd one is for confirm password.

       

 <tr>
            <td align="left" class="nbdcontent" style="width: 143px; height: 19px">
                Enter old password</td>
            <td align="center" class="nbdcontent" style="width: 10px; height: 19px">
                :</td>
            <td align="left" style="width: 100px; height: 19px">
                <asp:TextBox ID="txtOldpass" runat="server" CssClass="mytextbox" TextMode="Password"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvOldpass" runat="server" ControlToValidate="txtOldpass"
                    ErrorMessage="Please enter Old Password !">*</asp:RequiredFieldValidator></td>
        </tr>
        <tr>
            <td align="right" class="nbdcontent" style="height: 19px" colspan="3">
                         <asp:RegularExpressionValidator ID="Regex2" runat="server" ControlToValidate="txtNewPass"
    ValidationExpression="[a-z]+"
    ErrorMessage="Minimum 8 characters atleast 1 Alphabet, 1 Number and 1 Special Character" 
                             ForeColor="Red" >*</asp:RegularExpressionValidator>
            </td>
        </tr>
        <tr>
            <td align="left" class="nbdcontent" style="width: 143px; height: 19px">
                Enter new password</td>
            <td align="center" class="nbdcontent" style="width: 10px; height: 19px">
                :</td>
            <td align="left" style="width: 100px; height: 19px">
                <asp:TextBox ID="txtNewPass" runat="server" CssClass="mytextbox" 
                    TextMode="Password"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvNpass" runat="server" ControlToValidate="txtNewPass"
                    ErrorMessage="Please enter New Password !">*</asp:RequiredFieldValidator></td>
        </tr>
        <tr>
            <td align="right" class="nbdcontent" style="height: 19px" colspan="3">
                         <asp:RegularExpressionValidator ID="Regex3" runat="server" ControlToValidate="txtNCpass"
    ValidationExpression="^(?=.*[A-Za-z])(?=.*d)(?=.*[$@$!%*#?&])[A-Za-zd$@$!%*#?&]{8,}$"
    ErrorMessage="Minimum 8 characters atleast 1 Alphabet, 1 Number and 1 Special Character" 
                             ForeColor="Red" >*</asp:RegularExpressionValidator>
            </td>
        </tr>
        <tr>
            <td align="left" class="nbdcontent" style="width: 143px; height: 19px">
                Confirm password</td>
            <td align="center" class="nbdcontent" style="width: 10px; height: 19px">
                :</td>
            <td align="left" style="width: 100px; height: 19px">
                <asp:TextBox ID="txtNCpass" runat="server" CssClass="mytextbox" 
                    TextMode="Password"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvNCP" runat="server" ControlToValidate="txtNCpass"
                    ErrorMessage="Please enter confirm New  Password">*</asp:RequiredFieldValidator>
                <asp:CompareValidator ID="cvNPass" runat="server" ControlToCompare="txtNewPass" ControlToValidate="txtNCpass"
                    ErrorMessage="Password did not match !">*</asp:CompareValidator></td>
        </tr>
        <tr>
            <td align="left" class="nbdcontent" style="width: 143px; height: 19px">
            </td>
            <td align="center" class="nbdcontent" style="width: 10px; height: 19px">
            </td>
            <td align="left" style="width: 100px; height: 19px">
            </td>
        </tr>
        <tr>
            <td align="left" class="nbdcontent" style="width: 143px; height: 19px">
            </td>
            <td align="center" class="nbdcontent" style="width: 10px; height: 19px">
            </td>
            <td align="left" style="width: 100px; height: 19px">
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Update" />

Here I I have used RegularExpressionValidator  for password complexity. I am also encrypting the password word on client side.

below is my code for encrypting password.

<script type="text/javascript" src="../js/md5.js">  </script>
    <script type="text/javascript">

     function ChangePwd(salt) {

         var control = "<%=txtOldpass.ClientID %>";
         if (document.getElementById(control).value != "") {

             document.getElementById(control).value =
                    hex_md5(document.getElementById(control).value);

             document.getElementById(control).value =
                    hex_md5(document.getElementById(control).value + salt);
         }

         var control2 = "<%=txtNCpass.ClientID %>";
         if (document.getElementById(control2).value != "") {
         

             document.getElementById(control2).value =
                    hex_md5(document.getElementById(control2).value);
         }

         var control3 = "<%=txtNewPass.ClientID %>";
         if (document.getElementById(control3).value != "") {
           
             document.getElementById(control3).value =
                    hex_md5(document.getElementById(control3).value);
         }
     }
    </script>

and on page load

I am adding an attribute to the button

Button1.Attributes.Add("onclick", "return ChangePwd('" + salt.ToString() + "'); ToggleButton();");

The problem is :- as I click the button to change the password, it encrypt the entered string in the textboxes. As the password is encrypted now doesn’t meet the password complexity.

I want that, first it run the regular expression validator then it should encrypt the password.

Any Idea how to achieve this.

Hi demoninside,

Thanks for your post.

As for your problem, in fact, RegularExpressionValidator control validates the value of “txtNCpass” two times. The first time, when “txtNCpass” TextBox lose focus, RegularExpressionValidator control validates the value of “txtNCpass”. The second time, if
you click Button1, the page will refesh and RegularExpressionValidator control validates the value again. In this situation, encrypted password will not meet the password complexity.

To solve this problem, we could set IsValid property to true from behind code as below.

protected void Button1_Click(object sender, EventArgs e)
{
    Regex3.IsValid = true;
} 

Hope it will be helpful to you.

Best Regards,

Fei Han

Leave a Reply