Password must have at least one character without alpha

I need a regex for the password. Password must contain at least 8 characters. At least one character must be a number or a special character (not a letter).

[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 8)] [RegularExpression(@"(?=.*\W)?(?=.*\d)", ErrorMessage = "Error message")] [DataType(DataType.Password)] [Display(Name = "Password")] public string Password { get; set; } 

I have a length check, but I need help with a regular expression that checks if the password contains at least one number or a special character.

Valid passwords examples:

 testtest85* testtes* testtes1 test1234*+ 

Examples of invalid passwords:

 testtest testabc 
+7
source share
6 answers

Use regex pattern ^(?=.{8})(?=.*[^a-zA-Z])


Explanation:

 ^(?=.{8})(?=.*[^a-zA-Z]) β”‚β””β”€β”€β”¬β”€β”€β”€β”˜β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ β”” string contains some non-letter character β”‚ β”‚ β”‚ β”” string contains at least 8 characters β”‚ β”” begining of line/string 

If you want to limit also the maximum length (say 16), use a regex pattern:

 ^(?=.{8,16}$)(?=.*[^a-zA-Z]) 
+25
source

Run it through a fairly simple regular expression: [^a-zA-Z]

And then check the length separately:

 if(string.Length > 7) 
+2
source

A simple method would be:

 Match match1 = Regex.Match(<input_string>, @"(?=.{7})"); 

match1.Success guarantees that there are at least 8 characters.

 Match match2 = Regex.Match(<input_string>, [^a-zA-Z]); 

match2.Success ensures that there is at least one special character or number in the string.

 So, match1.Success && match2.Success guarantees will get what you want. 
+1
source

I tried the Omega example, but it did not work with my C # code. I recommend using this instead:

 [RegularExpression(@"^(?=[^\d_].*?\d)\w(\w|[ !@ #$%]){7,20}", ErrorMessage = @"Error. Password must have one capital, one special character and one numerical character. It can not start with a special character or a digit.")] 
0
source

An expression like:

 [a-zA-Z]*[0-9\+\*][a-zA-Z0-9\+\*]* 

should work fine (obviously, insert any additional special characters you want to allow or use the ^ operator to match anything other than letters / numbers); no need to use complex images. This approach makes sense if you only want to allow a specific subset of special characters that you know are β€œsafe” and prohibit everyone else.

If you want to include all special characters except certain ones that you know are β€œunsafe,” then it makes sense to use something like:

 \w[^\\]*[^a-zA-Z\\][^\\]* 

In this case, you explicitly prohibit the backslash in your password and allow any combination with at least one non-alphabetic character.

The above expression will match any string containing letters and at least one number or +, *. Regarding the requirement of β€œlength 8”, there really is no reason to verify that using regular expression.

-1
source
  function randomPassword(length) { var chars = "abcdefghijklmnopqrstuvwxyz#$%ABCDEFGHIJKLMNOP1234567890"; var pass = ""; for (var x = 0; x < length; x++) { var i = Math.floor(Math.random() * chars.length); pass += chars.charAt(i); } var pattern = false; var passwordpattern = new RegExp("[^a-zA-Z0-9+]+[0-9+]+[A-Z+]+[a-z+]"); pattern = passwordpattern.test(pass); if (pattern == true) { alert(pass); } else { randomPassword(length); } } 

try creating a random password with at least one special character

-1
source

Source: https://habr.com/ru/post/923384/


All Articles