I am using Asp.Net/C#, I am using RegularExpressionValidator to match a password field.My requirement is that I need the user to enter a password which will be atleast 7 characters long and it can contain any combination of alphabets and numeric characters , however it should contain strictly one non-alphanumeric character , can anybody suggest me as to how I can achieve this.
Any suggestions are welcome.Thank you
-
I hope the below link will be useful [Password validation][1] [1]: stackoverflow.com/questions/4352025/…Mathew Paul– Mathew Paul2012-04-23 05:46:59 +00:00Commented Apr 23, 2012 at 5:46
Add a comment
|
3 Answers
Try this
String[] words = { "Foobaar", "Foobar1", "1234567", "123Fooo", "Fo12!", "Foo12!", "Foobar123!", "!Foobar123", "Foo#bar123" };
foreach (String s in words) {
Match word = Regex.Match(s, @"
^ # Match the start of the string
(?= # positive look ahead
[\p{L}\p{Nd}]* # 0 or more letters or digits at the start
[^\p{L}\p{Nd}] # string contains exactly one non-digit, non-letter
[\p{L}\p{Nd}]* # 0 or more letters or digits after the special character
$) # till the end of the string
.{7,} # match 7 or more characters
$ # Match the end of the string
", RegexOptions.IgnorePatternWhitespace);
if (word.Success) {
Console.WriteLine(s + ": valid");
}
else {
Console.WriteLine(s + ": invalid");
}
}
Console.ReadLine();
\p{L} is a unicode code point with the property "letter"
\p{Nd} is a unicode code point with the property "digit"
2 Comments
Priyank Patel
can I use this with an asp.net regularexpression validator
stema
I don't know asp.net, but I tested it with c#, so since both is .net it should work.
((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{7,20})
( # Start of group
(?=.*\d) # must contains one digit from 0-9
(?=.*[a-z]) # must contains one lowercase characters
(?=.*[A-Z]) # must contains one uppercase characters
(?=.*[@#$%]) # must contains one special symbols in the list "@#$%"
. # match anything with previous condition checking
{7,20} # length at least 7 characters and maximum of 20
)
Copied from here.
11 Comments
Priyank Patel
can you suggest me to allow the user to enter only non aplhanumeric character
Jack
[^\w\d] regex should do it.Robin Maben
@freebird: You mean.. only 1 non-alphanumeric? I can edit the answer if that's what you need. Else, Jack's comment should do it.
Priyank Patel
@Jack i think it will words and digits , how do I include only one non alphanumeric character as well
Priyank Patel
@RobinMaben , I meant that it should contain words digits and only one nonalphanumeric character
|
I used the following to match my needs ^([\w]*[(\W)]{1}[\d]*)$ , this allows a password to start with an alphabet any number of times , then the password must contain an non-alphanumeric character only 1 time , followed by a digits any number of times.
2 Comments
stema
cool that you came up with an own solution. I assume that you do the length check then somewhere else. Some hints:
{1} is superfluous, you simply don't need it, you don't need the round brackets inside a character class, when your char class has only one member you don't need a class, \w contains also the underscore and at last: you don't need the brackets around the whole expression. So you could simplify your expression to ^\w*\W\d*$ Priyank Patel
@Stema thanks a lot I will simplify the expression , My length check is done in the configuration file , the pattern I used in my answer will only allow passwords like abc@123 , xyz#456 etc.thanks