Verification password contains alphanumeric and special character

How do you check if passwordText string contains at least

  • 1 alphabet character
  • 1 number
  • 1 special character (character)
+2
source share
7 answers

Try the following:

bool result = passwordText.Any(c => char.IsLetter(c)) && passwordText.Any(c => char.IsDigit(c)) && passwordText.Any(c => char.IsSymbol(c)); 

Although you can be a little more specific about what you mean by the symbol โ€œalphabetโ€, โ€œnumberโ€ and โ€œsymbolโ€, because these terms mean different things for different people, and this does not mean that your definition of these conditions corresponds definitions used by the framework.

I would suggest that by letter you mean "az" or "AZ", by digit means "0-9", and by character you mean any other printable ASCII character. If yes, try the following:

 static bool IsLetter(char c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } static bool IsDigit(char c) { return c >= '0' && c <= '9'; } static bool IsSymbol(char c) { return c > 32 && c < 127 && !IsDigit(c) && !IsLetter(c); } static bool IsValidPassword(string password) { return password.Any(c => IsLetter(c)) && password.Any(c => IsDigit(c)) && password.Any(c => IsSymbol(c)); } 

If you really mean something else, then adjust the above methods accordingly.

+12
source

Using RegEx will be the most flexible. You can always save it in the resource file and modify it without making any changes to the code.

See below for examples of passwords;

http://regexlib.com/Search.aspx?k=password

+2
source

You can use String.IndexOfAny() to determine if at least one character exists in the specified character array in the string.

+1
source
 strPassword.IndexOfAny(new char['!','@','#','$','%','^','&','*','(',')']); 

You can run this with the array of characters / characters you are looking for. If it returns something greater than -1, then you have a match. The same thing can be done with numbers.

This will allow you to pinpoint what you are looking for, and easily allow you to exclude certain characters if you wish.

+1
source

It is pretty simple:

 Regex sampleRegex = new Regex(@"(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{2,})$"); boolean isStrongPassword= sampleRegex.IsMatch(givenPassword); 
+1
source

For something pretty simple, I took this from the program I used earlier.

  //password rules int minUpper = 3; int minLower = 3; int minLength = 8; int maxLength = 12; string allowedSpecials = "@#/.!')"; //entered password string enteredPassword = " TEStpass123@ "; //get individual characters char[] characters = enteredPassword.ToCharArray(); //checking variables int upper = 0; int lower = 0; int character = 0; int number = 0; int length = enteredPassword.Length; int illegalCharacters = 0; //check the entered password foreach (char enteredCharacters in characters) { if (char.IsUpper(enteredCharacters)) { upper = upper + 1; } else if (char.IsLower(enteredCharacters)) { lower = lower + 1; } else if (char.IsNumber(enteredCharacters)) { number = number + 1; } else if (allowedSpecials.Contains(enteredCharacters.ToString())) { character = character + 1; } else { illegalCharacters = illegalCharacters + 1; } // MessageBox.Show(chars.ToString()); } if (upper < minUpper || lower < minLower || length < minLength || length > maxLength || illegalCharacters >=1) { MessageBox.Show("Something not right, your password does not meet the minimum security criteria"); } else { //code to proceed this step } 
+1
source

This should meet all your requirements. It does not use regex.

 private bool TestPassword(string passwordText, int minimumLength=5, int maximumLength=12,int minimumNumbers=1, int minimumSpecialCharacters=1) { //Assumes that special characters are anything except upper and lower case letters and digits //Assumes that ASCII is being used (not suitable for many languages) int letters = 0; int digits = 0; int specialCharacters = 0; //Make sure there are enough total characters if (passwordText.Length < minimumLength) { ShowError("You must have at least " + minimumLength + " characters in your password."); return false; } //Make sure there are enough total characters if (passwordText.Length > maximumLength) { ShowError("You must have no more than " + maximumLength + " characters in your password."); return false; } foreach (var ch in passwordText) { if (char.IsLetter(ch)) letters++; //increment letters if (char.IsDigit(ch)) digits++; //increment digits //Test for only letters and numbers... if (!((ch > 47 && ch < 58) || (ch > 64 && ch < 91) || (ch > 96 && ch < 123))) { specialCharacters++; } } //Make sure there are enough digits if (digits < minimumNumbers) { ShowError("You must have at least " + minimumNumbers + " numbers in your password."); return false; } //Make sure there are enough special characters -- !(a-zA-Z0-9) if (specialCharacters < minimumSpecialCharacters) { ShowError("You must have at least " + minimumSpecialCharacters + " special characters (like @,$,%,#) in your password."); return false; } return true; } private void ShowError(string errorMessage) { Console.WriteLine(errorMessage); } 

This is an example of what to call it:

 private void txtPassword_TextChanged(object sender, EventArgs e) { bool temp = TestPassword(txtPassword.Text, 6, 10, 2, 1); } 

This does not check for a mixture of upper and lower case letters, which may be preferable. To do this, simply fold the char condition up, where (ch > 96 && ch < 123) is a set of lowercase letters, and (ch > 64 && ch < 91) is uppercase.

Regex is shorter and simpler (for those who are good with it), and there are many examples on the Internet, but this method is better for setting feedback for the user.

0
source

All Articles