In this article I will explain with an example, how to implement Password Policy using Regular Expression (Regex) in Windows Forms (WinForms) Application using C# and VB.Net.
When the Button is clicked, the TextBox will be validated using Regular Expression (Regex) in C# and VB.Net.
In this article following validation will be performed.
1. Minimum 8 characters at least 1 Alphabet and 1 Number.
2. Minimum 8 characters at least 1 Alphabet, 1 Number and 1 Special Character.
3. Minimum 8 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet and 1 Number.
4. Minimum 8 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 1 Number and 1 Special Character.
5. Minimum 8 and Maximum 10 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 1 Number and 1 Special Character.
Form Design
The following Form consists of some TextBoxes, Labels and Button controls.
Namespaces
You will need to import the following namespace.
C#
using System.Text.RegularExpressions;
VB.Net
Imports System.Text.RegularExpressions
1. Validating minimum 8 characters, at-least 1 Alphabet and 1 Number
Regular Expression (Regex)
^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$
Explanation
Above Regular Expression validates minimum 8 characters, at-least 1 Alphabet and 1 Number.
Example
When the Validate Button is clicked, the value of the TextBox is validated against the Regular Expression (Regex) and if invalid, the error Label is displayed.
Valid Example: pass1234 OR PaSs1234 OR PASS1234
C#
private void OnValidate1(object sender, EventArgs e)
{
lblMessage1.Hide();
Regex regex1 = new Regex(@"^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$");
if (!regex1.IsMatch(txt1.Text.Trim()))
{
lblMessage1.Show();
}
}
VB.Net
Private Sub OnValidate1(sender As Object, e As EventArgs) Handles btnValidate1.Click
lblMessage1.Hide()
Dim regex1 As Regex = New Regex("^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$")
If Not regex1.IsMatch(txt1.Text.Trim()) Then
lblMessage1.Show()
End If
End Sub
2. Validating minimum 8 characters, at-least 1 Alphabet, 1 Number and 1 Special Character
Regular Expression (Regex)
^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,}$
Explanation
Above Regular Expression validates minimum 8 characters, at-least 1 Alphabet, 1 Number and 1 Special Character.
Example
When the Validate Button is clicked, the value of the TextBox is validated against the Regular Expression (Regex) and if invalid, the error Label is displayed.
Valid Example: pass@123 OR PaSS#123 OR PASS@123
C#
private void OnValidate2(object sender, EventArgs e)
{
lblMessage2.Hide();
Regex regex2 = new Regex(@"^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,}$");
if (!regex2.IsMatch(txt2.Text.Trim()))
{
lblMessage2.Show();
}
}
VB.Net
Private Sub OnValidate2(sender As Object, e As EventArgs) Handles btnValidate2.Click
lblMessage2.Hide()
Dim regex2 As Regex = New Regex("^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,}$")
If Not regex2.IsMatch(txt2.Text.Trim()) Then
lblMessage2.Show()
End If
End Sub
3. Validating minimum 8 characters, at-least 1 Uppercase Alphabet, 1 Lowercase Alphabet and 1 Number
Regular Expression (Regex)
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$
Explanation
Above Regular Expression validates minimum 8 characters, at-least 1 Uppercase Alphabet, 1 Lowercase Alphabet and 1 Number.
Example
When the Validate Button is clicked, the value of the TextBox is validated against the Regular Expression (Regex) and if invalid, the error Label is displayed.
Valid Example: PaSs1234 OR pASS1234
C#
private void OnValidate3(object sender, EventArgs e)
{
lblMessage3.Hide();
Regex regex3 = new Regex(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$");
if (!regex3.IsMatch(txt3.Text.Trim()))
{
lblMessage3.Show();
}
}
VB.Net
Private Sub OnValidate3(sender As Object, e As EventArgs) Handles btnValidate3.Click
lblMessage3.Hide()
Dim regex3 As Regex = New Regex("^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$")
If Not regex3.IsMatch(txt3.Text.Trim()) Then
lblMessage3.Show()
End If
End Sub
4. Validating minimum 8 characters, at-least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 1 Number and 1 Special Character
Regular Expression (Regex)
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,}
Explanation
Above Regular Expression validates minimum 8 characters, at-least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 1 Number and 1 Special Character.
Example
When the Validate Button is clicked, the value of the TextBox is validated against the Regular Expression (Regex) and if invalid, the error Label is displayed.
Valid Example: PaSs@123 OR pAss@123
C#
private void OnValidate4(object sender, EventArgs e)
{
lblMessage4.Hide();
Regex regex4 = new Regex(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,}");
if (!regex4.IsMatch(txt4.Text.Trim()))
{
lblMessage4.Show();
}
}
VB.Net
Private Sub OnValidate4(sender As Object, e As EventArgs) Handles btnValidate4.Click
lblMessage4.Hide()
Dim regex4 As Regex = New Regex("^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,}")
If Not regex4.IsMatch(txt4.Text.Trim()) Then
lblMessage4.Show()
End If
End Sub
5. Validating minimum 8 and Maximum 10 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 1 Number and 1 Special Character
Regular Expression (Regex)
^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$
Explanation
Above Regular Expression validates minimum 8 and Maximum 10 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 1 Number and 1 Special Character.
Example
When the Validate Button is clicked, the value of the TextBox is validated against the Regular Expression (Regex) and if invalid, the error Label is displayed.
Valid Example: PaSs@123
C#
private void OnValidate5(object sender, EventArgs e)
{
lblMessage5.Hide();
Regex regex5 = new Regex(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,10}");
if (!regex5.IsMatch(txt5.Text.Trim()))
{
lblMessage5.Show();
}
}
VB.Net
Private Sub OnValidate5(sender As Object, e As EventArgs) Handles btnValidate5.Click
lblMessage5.Hide()
Dim regex5 As Regex = New Regex("^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,10}")
If Not regex5.IsMatch(txt5.Text.Trim()) Then
lblMessage5.Show()
End If
End Sub
Screenshots
Invalid Password
Valid password
Demo
Downloads