0

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

1

3 Answers 3

3

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"

Sign up to request clarification or add additional context in comments.

2 Comments

can I use this with an asp.net regularexpression validator
I don't know asp.net, but I tested it with c#, so since both is .net it should work.
1
((?=.*\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

can you suggest me to allow the user to enter only non aplhanumeric character
[^\w\d] regex should do it.
@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.
@Jack i think it will words and digits , how do I include only one non alphanumeric character as well
@RobinMaben , I meant that it should contain words digits and only one nonalphanumeric character
|
0

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

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*$
@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.