Click here to Skip to main content
16,022,971 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how i can validate password length in asp.net??
and also compare password nd confirm password..
Posted
Updated 10-Sep-17 22:50pm

Your question is quite ambigious since you did not mention if you're using Web Forms or MVC. Nevertheless, you can use client-side validation in javascript with RegEx.test method since it can be used in both Web Forms and MVC.

JavaScript
var hasError = false;
var password = $('#password').val();
if (password.length > 0) {
    var passwordTest = new RegExp('^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[~`!@#\$%\^&\*,.])(?=.{8,})');
    if (!passwordTest.test(password)) {
        hasError = true;
    }
}

RegEx Description:
^	            The password string will start this way
(?=.*[a-z])	    The string must contain at least 1 lowercase alphabetical character
(?=.*[A-Z])	    The string must contain at least 1 uppercase alphabetical character
(?=.*[0-9])	    The string must contain at least 1 numeric character
(?=.*[!@#\$%])	The string must contain at least one special character
(?=.{8,})	    The string must be eight characters or longer
 
Share this answer
 
Comments
Kats2512 11-Sep-17 5:50am    
just 5 years too late.
Cristian Mayo 11-Sep-17 6:01am    
yeah, I noticed the post date late.. haha
Kats2512 11-Sep-17 6:04am    
next time just check, your account could get reported and your answer marked as spam.
Cristian Mayo 11-Sep-17 6:05am    
Noted. I joined just today and the question is still active. I appreciate you warning though.

Thanks. ^_^
XML
<asp:Label ID="lbl_password" runat="server" CssClass="Label" Text="Password"></asp:Label>
            <div class="cleaner">
            </div>
            <asp:TextBox ID="radtxtPassword" runat="server" TextMode="Password" CssClass="textbox" ValidationGroup="RegisterCheck">
            </asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="radtxtPassword"
                ValidationGroup="RegisterCheck" ></asp:RequiredFieldValidator>

            <asp:RegularExpressionValidator ID="valPassword" runat="server" ControlToValidate="radtxtPassword"
                ErrorMessage="Minimum password length is 6" ValidationExpression="^([a-zA-Z0-9@#$%^&+=*]{6,30})$"
                ValidationGroup="RegisterCheck" />
            <div class="cleaner_h10">
            </div>
            <asp:Label ID="lbl_ConfirmPassword" runat="server" CssClass="Label" Text="Confirm Password"></asp:Label>
            <div class="cleaner">
            </div>
            <asp:TextBox ID="radtxtConfirmPassword" runat="server" TextMode="Password" CssClass="textbox"
                ValidationGroup="RegisterCheck">
            </asp:TextBox>

            <asp:CompareValidator ID="cmpvldPassword" runat="server" ControlToCompare="radtxtConfirmPassword"
                ControlToValidate="radtxtPassword" Type="String" Operator="Equal" ValidationGroup="RegisterCheck"
                ErrorMessage="Password should match"></asp:CompareValidator>


VB
<asp:Button ID="btnCreateuser" runat="server" CssClass="complete_btn" ValidationGroup="RegisterCheck"
                BorderStyle="None" OnClick="btnCreateuser_Click" CausesValidation="true" />
 
Share this answer
 
Have a look:
How to Use the ASP.NET Validation Control to Validate the User Input[^]

Try this code:
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");
}


Refer: Compare password[^]
 
Share this answer
 
v2
Comments
__TR__ 5-Sep-12 5:59am    
+5
Prasad_Kulkarni 5-Sep-12 6:04am    
Thank you TR!

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