Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,
I have two textbox in my form.one is password another one is confirm password.now we'll compare both textbox.how we compare those textbox?
Posted

XML
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
        <asp:TextBox ID="txtConfirmPassword" runat="server"></asp:TextBox>
        <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txtPassword"
            ControlToValidate="txtConfirmPassword" ErrorMessage="CompareValidator"></asp:CompareValidator>
 
Share this answer
 
<asp:textbox id="txtConfirmPassword" validationgroup="a" cssclass="textbox" textmode="Password" xmlns:asp="#unknown">
                                             runat="server" Columns="20"></asp:textbox>

<asp:comparevalidator enableviewstate="false" display="None" validationgroup="a" xmlns:asp="#unknown">
                                             ControlToValidate="txtConfirmPassword" runat="server" ErrorMessage="Your passwords do not match. Please type more carefully."
                                             ID="CompareConfirm" ControlToCompare="txtNewPassword">
                                         </asp:comparevalidator>



You can use Ajax "ValidatorCalloutExtender" to make the looks better

XML
<ajaxToolkit:ValidatorCalloutExtender EnableViewState="false" ID="ValidatorCalloutExtender5"
                                              TargetControlID="CompareConfirm" HighlightCssClass="validatorCalloutHighlight"
                                              runat="server">
                                          </ajaxToolkit:ValidatorCalloutExtender>



And for windows you can use this function to validate

Private Function ChangePassword() As Boolean
        Try
            Dim strMSG As String = ""
            Dim intError As Integer = 0
            If txtOldPass.Text.Trim.Equals("") Then
                strMSG = "Old password can't be blank"
                intError = 1
            ElseIf txtNewPass.Text.Trim.Equals("") Then
                strMSG = "New password can't be blank"
                intError = 1
            ElseIf txtConNewPass.Text.Trim.Equals("") Then
                strMSG = "Confirm password can't be blank"
                intError = 1
            ElseIf Not txtOldPass.Text.Trim.Equals(strOLDPassword) Then
                strMSG = "Old Password is incorrect"
                intError = 1
            ElseIf Not txtNewPass.Text.Trim.Equals(txtConNewPass.Text.Trim) Then
                strMSG = " New Password and Confirm New Password do not Match"
                intError = 1
            ElseIf txtNewPass.Text.Trim.Length < 6 Then
                strMSG = "New password can't be less than 6 characters"
                intError = 1
            ElseIf txtNewPass.Text.Trim.Equals(strOLDPassword) Then
                strMSG = "New password can't be same as old password"
                intError = 1
            End If
            If intError = 1 Then
                MessageBox.Show(strMSG, "Change Password", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return False
            End If

            Dim arrColName As New ArrayList
            Dim arrRowData As New ArrayList
            arrColName.Add("UM_VC30_Password")
            arrRowData.Add(Encrypt(txtNewPass.Text.Trim))

            Dim strSelectSQL As String
            strSelectSQL = "select * from table where UM_IN4_Address_No_FK=" & cbUserName.SelectedValue

            If objWS.Update(PropConString, "Table", strSelectSQL, arrColName, arrRowData, "frm_ChangePassword-ChangePassword-75") = True Then
                MessageBox.Show("Password Changed Successfully", "Password Changed", MessageBoxButtons.OK, MessageBoxIcon.Information)
                strOLDPassword = txtNewPass.Text.Trim
                txtOldPass.Text = ""
                txtNewPass.Text = ""
                txtConNewPass.Text = ""
                Return True
            Else
                MessageBox.Show("Please Try Later", "Try Later", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return False
            End If
            Return True
        Catch ex As Exception
            XYZ.Logging.EventLogging.CreateLog("frm_ChangePassword", "ChangePassword-88", LogType.Application, LogSubType.Exception, ex.TargetSite.Attributes, ex.ToString, PropUserID, PropUserName)
        End Try

    End Function


And Button you can use this code

VB
Private Sub btnChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChange.Click
     ChangePassword()
 End Sub
 
Share this answer
 
v3
Comments
Hari Krishna Prasad Inakoti 11-Apr-12 1:37am    
iam using windows application dude..
C#
public bool IsPasswordsEqual(string password1, string password2)
{
	if (password1.Equals(password2))
        {
		return true;
	}

	return false;
}


use this method on submit button click event or confirmpassword textbox leave event
as
C#
if(!IsPasswordsEqual(Textbox1.Text,TextBox2.Text))
{
MessageBox.Show("Enter same password in both");
}
 
Share this answer
 
v2
Comments
Hari Krishna Prasad Inakoti 11-Apr-12 1:47am    
it is not working uma shankar
Prasad_Kulkarni 11-Apr-12 1:49am    
Can you post your code, how/ what you have tried so far?
uspatel 11-Apr-12 1:52am    
See updated answer....
if any error reply me.
Prasad_Kulkarni 11-Apr-12 2:02am    
It will work for sure
Hari Krishna Prasad Inakoti 11-Apr-12 2:07am    
i got error at !IsPasswordsEqual

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900