0

I can't seem to find any suitable answer for this.

I need a regex that will make sure that there is at least one special character, one number, one upper and one lower and at least 8 long.

I have a few here but for some reason, when I add more characters it doesn't recognize them.

ValidationExpression="^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%.*?&]{8,}" 

I want to add every single special character.

Joseph
  • 80
  • 14
  • 3
    I don't know if I would make this a regex since any of those characters can appear anywhere in your string, so it's not really a pattern. I would probably just issue separate checks for each requirement of the string to validate. I know it's not a neat little one-liner, but at least it would be readable. – Doug F Feb 28 '19 at 14:05
  • Do you mean you want to match any chars but whitespace? Replace `[A-Za-z\d$@$!%.*?&]` with `\S` – Wiktor Stribiżew Feb 28 '19 at 14:11
  • I have two input strings and these are checking that at least one of the above characters exist in the input string. But i want to add all of the special characters available to check that at least the user has used at least one of them. – domddidlydom1233 Feb 28 '19 at 14:13
  • So in essence, i want to add !"£$%^&*()_+{}:@~<>?|-=[];'#,./\ also – domddidlydom1233 Feb 28 '19 at 14:15
  • Possible duplicate of [Checking strings for a strong enough password](https://stackoverflow.com/questions/12899876/checking-strings-for-a-strong-enough-password) – 41686d6564 stands w. Palestine Feb 28 '19 at 14:26

3 Answers3

0

Please try to avoid long and complex regular expressions, because it would be hard to understand and modify them in future. Instead define a few simple and straightforward Regex, then combine it with c# code

var hasLowercaseChar = new Regex("[a-z]");
var hasUppercaseChar = new Regex("[A-Z]");
var hasDigitChar = new Regex("[0-9]");
var hasSpecialChar = new Regex("\\$!\\^-@%&\\.\\*");

var input = "" // your input string here
var passwordIsValid = hasLowercaseChar.IsMatch(input)
    && hasUppercaseChar.IsMatch(input)
    && hasDigitChar.IsMatch(input)
    && hasSpecialChar.IsMatch(input)
    && input.Length >= 8;

In above example each regular expression is simple and even if you not sure what it should match you can infer this from variable names

Aleks Andreev
  • 7,016
  • 8
  • 29
  • 37
0

The following regex should do the trick: ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*\W)[\S]{8,}$

0

Try Regex: (?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@!%*?&!"£$%^&*()_+{}:@~<>?|=[\];'#,.\/\\-])[A-Za-z\d$@!%*?&!"£$%^&*()_+{}:@~<>?|=[\];'#,.\/\\-]{8,}

Demo

Matt.G
  • 3,586
  • 2
  • 10
  • 23