Click here to Skip to main content
15,905,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to validate password to allow only letters and special character , for this i used Regular expresion validation but it was not properly working like i used ^[a-zA-Z!@#$%^&*]+$ but this code what fail when i entere only letter it was not throwing any type of err ..
Posted
Comments
OriginalGriff 21-Apr-13 9:39am    
I think we need more information, really.
Perhaps an example of the inputs you gave, and what happened, together with what you expected to happen.
The relevant code fragment would probably help too.
Use the "Improve question" widget to edit your question and provide better information.
Alok.singh1166 21-Apr-13 12:25pm    
Hi OriginalGriff according to my requirement in password txt field user should enter at least one upper case ,one lower case and one special character .at least one is mandatory until & unless it should throw error.pls help

1 solution

As i understand you want at least one letter and at least one special character ..then try this regex..hope this will help
C#
if (System.Text.RegularExpressions.Regex.IsMatch("$#%hg#$Y%#$", @"^(?=.*[a-zA-Z])(?=.*[!@#$%^&*])"))
                MessageBox.Show("Match found");
            else
                MessageBox.Show("Match not found");

Updated answer
for "Enter at least one uppercase and leter in uppercase and lower case & special character"
C#
if (System.Text.RegularExpressions.Regex.IsMatch("$#%hg#$Y%#$", @"^(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*])"))
               MessageBox.Show("Match found");
           else
               MessageBox.Show("Match not found");
 
Share this answer
 
v2
Comments
Alok.singh1166 21-Apr-13 11:35am    
THANK'S PALLAVI
this is the code i am using but what i want in password txt field user there should be at least one upper case one lower case and one special character .but this regex expresion is not throwing any error while entering only charaters.pls help


private void txtpassword(object sender, EventArgs e)
{
try
{
if (System.Text.RegularExpressions.Regex.IsMatch("$#%hg#$Y%#$", @"^(?=.*[a-zA-Z])(?=.*[!@#$%^&*])"))
return;

}
catch (Exception ey)
{
label3.Visible = true;
label3.Text = "Enter at least one uppercase and leter in uppercase and lower case & special character";
}
}
Pallavi Waikar 21-Apr-13 11:48am    
it's easy only u have to separate (?=.*[a-zA-Z]) into (?=.*[a-z])(?=.*[A-Z]) ...because then after upper and lower letter will checked independently ..check updated answer
Alok.singh1166 21-Apr-13 12:11pm    
Hi pallavi i used this code but its not what i want .according to my need user should enter atleast one upper case,lower case and special character.if user entered only uppercase and not entered any special character it should throw error..
Pallavi Waikar 21-Apr-13 12:32pm    
OK..try this code
private void txtpassword(object sender, EventArgs e)
{
try
{
if (System.Text.RegularExpressions.Regex.IsMatch("$#%hg#$Y%#$", @"^(?=.*[a-zA-Z])(?=.*[!@#$%^&*])"))
return;
else
{
label1.Visible = true;
label1.Text = "Enter at least one uppercase and leter in uppercase and lower case & special character";
}


}
catch (Exception ey)
{
label3.Visible = true;
label3.Text = "Enter at least one uppercase and leter in uppercase and lower case & special character";
}
Alok.singh1166 21-Apr-13 12:40pm    
I entered only letters no special character i used and it is not throwing any error.

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