I need a regular expression for a password. The password has to 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 validation, but I need help with a regular expression that checks if the password contains at least one number or special character.
Examples of valid passwords:
testtest85*
testtes*
testtes1
test1234*+
Examples of not valid passwords:
testtest
testabc
This question is related to
c#
regex
asp.net-mvc
validation
A simple method will be like this:
Match match1 = Regex.Match(<input_string>, @"(?=.{7})");
match1.Success ensures 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 within the string.
So, match1.Success && match2.Success guarantees will get what you want.
I tried Omega's example however it was not working 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.")]
An expression like this:
[a-zA-Z]*[0-9\+\*][a-zA-Z0-9\+\*]*
should work just fine (obviously insert any additional special characters you want to allow or use ^ operator to match anything except letters/numbers); no need to use complicated lookarounds. This approach makes sense if you only want to allow a certain subset of special characters that you know are "safe", and disallow all others.
If you want to include all special characters except certain ones which you know are "unsafe", then it makes sense to use something like:
\w[^\\]*[^a-zA-Z\\][^\\]*
In this case, you are explicitly disallowing backslashes in your password and allowing any combination with at least one non-alphabetic character otherwise.
The expression above will match any string containing letters and at least one number or +,*. As for the "length of 8" requirement, theres really no reason to check that using regex.
Run it through a fairly simple regex: [^a-zA-Z]
And then check it's length separately:
if(string.Length > 7)
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 this to create the random password with atleast one special character
Source: Stackoverflow.com