Reputation: 6996
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
Upvotes: 0
Views: 1665
Reputation: 6996
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.
Upvotes: 0
Reputation: 92976
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"
Upvotes: 3
Reputation: 23044
((?=.*\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.
Upvotes: 1