Password Validation in C#
Submitted by janobe on Saturday, May 18, 2019 - 01:22.
Today, you will learn how to create a password validation textbox in c#. This procedure illustrates how to validate a textbox at least 8 to 15 charters and it's composed of at least one upper case and numbers. This is very helpful for you when you have to restrict your textbox password.
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.
Creating Application
Step 1
Open Microsoft Visual Studio 2015 and create a new windows form application for c#.Step 2
Do the form just like shown below.Step 3
Press F7 to open the code editor. In the code editor, add a namespace to accessregular expression
libraries.
- using System.Text.RegularExpressions;
Step 4
Create a method for password validation.- private static Regex PasswordValidation()
- {
- string pattern = "(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,15})$";
- }
Step 5
Initialize the method that you have created.- static Regex vaildate_password = PasswordValidation();
Step 6
Write the following code to verify a password in a textbox when the button is clicked.- private void button1_Click(object sender, EventArgs e)
- {
- if (vaildate_password.IsMatch(textBox1.Text) != true)
- {
- MessageBox.Show("Password must be atleast 8 to 15 characters. It contains atleast one Upper case and numbers.", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- textBox1.Focus();
- return;
- }
- else
- {
- MessageBox.Show("Password is correct.");
- }
- }
Add new comment
- 2103 views