Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HI all

I have validate password. password length should be min 6 and should contain atleast one digit and atleast one alphabet.

Please help me out

I have tried following function but it fails
C#
public bool CheckPassword(string password)
        {
            //string MatchEmailPattern = "(?=.{6,})[a-zA-Z0-9]+[^a-zA-Z]+|[^a-zA-Z]+[a-zA-Z]+";
            string MatchEmailPattern = "^[a-zA-Z0-9]+$";

                if (password != null) return Regex.IsMatch(password, MatchEmailPattern);
                else return false;


        }
Posted
Updated 30-May-18 7:56am
Comments
Dhanushka Madushan lk 20-Apr-12 6:13am    
check this
http://www.codeproject.com/Articles/367001/Introduction-to-ASP-NET-validation

Please refer this answer[^]-Regular Expressions for password
 
Share this answer
 
The following regular expression can be used to match a string whose length is at least 6, contains at least one digit and contains at least one lower case or upper case alphabet

(?=^.{6,}$)(?=.*\d)(?=.*[a-zA-Z])

However, I think it is good idea to avoid spaces in the string, in which case the following regular expression can be used

(?=^[^\s]{6,}$)(?=.*\d)(?=.*[a-zA-Z])

It can be tested on line here
http://regexhero.net/tester/[^]
 
Share this answer
 
Comments
Pravinjas 20-Apr-12 8:39am    
Thanks...
VJ Reddy 20-Apr-12 10:33am    
You're welcome and thank you for the response.
Sergey Alexandrovich Kryukov 22-Apr-12 21:38pm    
Answered, a 5.
--SA
VJ Reddy 22-Apr-12 21:48pm    
Thank you, SA.
(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,15})$
This requires at least one digit, at least one alphabetic character, no special characters, and from 6-15 characters in length.

take a look for
http://www.regular-expressions.info/reference.html[^]

http://msdn.microsoft.com/en-us/library/ms998267.aspx[^]
 
Share this answer
 

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