In this article I will explain with an example, how to perform Alphabets (Upper and Lower case) validation using
Regular Expressions in Windows Forms (WinForms) Application using C# and VB.Net.
Form Design
The following Form consists of:
TextBox – For capturing Alphabets.
Label – For showing error message.
Button – For validating Alphabets.
The Button has been assigned with the Click event handler.
Namespaces
You will need to import the following namespace.
C#
using System.Text.RegularExpressions;
VB.Net
Imports System.Text.RegularExpressions
Regular Expression (Regex) to validate Alphabets (Upper and Lower case)
Regular Expression (Regex)
^[a-zA-Z]+$
Explanation
Example
When the
Validate Button is clicked, the value of the TextBox is validated against the
Regular Expressions (Regex) and if invalid, the error message is displayed.
C#
private void OnValidate(object sender, EventArgs e)
{
lblError.Hide();
Regex regex = new Regex("^[a-zA-Z]+$");
if (!regex.IsMatch(txtAlphabets.Text.Trim()))
{
lblError.Show();
}
}
VB.Net
Private Sub OnValidate(sender As Object, e As EventArgs) Handles btnValidate.Click
lblError.Hide()
Dim regex As Regex = New Regex("^[a-zA-Z]+$")
If Not regex.IsMatch(txtAlphabets.Text.Trim()) Then
lblError.Show()
End If
End Sub
Screenshots
Invalid Value
Valid Value
Demo
Downloads