In this article I will explain with an example, how to implement Indian PAN Card Number validation in Windows Forms (WinForms) Application using C# and VB.Net.
When the Submit Button is clicked, the PAN Card Number in TextBox will be validated using Regular Expression (Regex) using C# and VB.Net.
Form Design
The Form Design consists of a TextBox, a Label and a Button.
Namespaces
You will need to import the following namespace.
C#
using System.Text.RegularExpressions;
VB.Net
Imports System.Text.RegularExpressions
Validating PAN Card Number in Windows Forms Application using C# and VB.Net
Inside the Form Load event, the TextBox is assigned Upper Case CharacterCasing, this will make sure that the PAN Card Number entered is always in Upper Case.
When the Submit Button is clicked, the value of the PAN Card Number TextBox is validated against the Regular Expression (Regex) and if invalid, the Error Label is displayed.
C#
private void Form1_Load(object sender, EventArgs e)
{
txtPANCardNumber.CharacterCasing = CharacterCasing.Upper;
}
private void btnSubmit_Click(object sender, EventArgs e)
{
lblError.Hide();
Regex regex = new Regex("([A-Z]){5}([0-9]){4}([A-Z]){1}$");
if(!regex.IsMatch(txtPANCardNumber.Text.Trim()))
{
lblError.Show();
}
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
txtPANCardNumber.CharacterCasing = CharacterCasing.Upper
End Sub
Private Sub btnSubmit_Click(sender As System.Object, e As System.EventArgs) Handles btnSubmit.Click
lblError.Hide()
Dim regex As Regex = New Regex("([A-Z]){5}([0-9]){4}([A-Z]){1}$")
If Not regex.IsMatch(txtPANCardNumber.Text.Trim()) Then
lblError.Show()
End If
End Sub
Screenshots
Invalid PAN Card Number
Valid PAN Card Number
Downloads