How to validate a string in textbox control for password in C#


Here I have given an example for a password validation code snippet by using very simple and basic common algorithm through textbox control in C#. In this rules of password validation, you are allowed to type any alphabet, numeric and special characters as password value.

Log in or user name password code snippet is essential module for all projects. Alphabet A-Z, a-z numeric 0-9 and all special characters are used for password digit. Password length should be at least 8 digits and no repeating digit is allowed. In order to check password digit, ASCII code of alphanumeric and special characters are used.

        private void textBox1_TextChanged(object sender, EventArgs e)
{
label1.Text = PasswordValidation(textBox1.Text);
}

private string PasswordValidation(string value)
{
byte[] asciiBytes = Encoding.ASCII.GetBytes(value);
if (value.Length < 8)
return "Length must greater then 7";
else
return "";
foreach (byte b in asciiBytes)
{
if ((b >= 65 && b <= 90) || (b >= 97 && b <= 122) || (b >= 48 && b <= 57) || (b >= 32 && b <= 47))
{
foreach (char c in value.Remove(value.Length - 1, 1))
{
if (value.Remove(0, value.Length - 1) == c.ToString())
return "Error";
}
}
else
return "Error";
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: