In this article I will explain with an example, how to perform Mobile Number (Cellphone Number) TextBox validation i.e. exact Ten Numbers (Digits) validation using Regular Expression in Windows Forms (WinForms) Application using C# and VB.Net.
 
 
Form Design
The following Form consists of two Labels, one TextBox and a Button control.
Mobile Number (Cellphone Number) Validation using RegularExpressions (Regex) in C# and VB.Net
 
 
Namespaces
You will need to import the following namespace.
C#
using System.Text.RegularExpressions;
 
VB.Net
Imports System.Text.RegularExpressions
 
 
Validating Mobile Number (Cellphone Number)
Regular Expression (Regex)
^([0-9]{10})$
 
Explanation
Above Regular Expression validates number contains only ten digits without alphabets, and without having space between digits.
 
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.
C#
private void OnValidate(object sender, EventArgs e)
{
    lblMessage.Hide();
    Regex regex1 = new Regex("^([0-9]{10})$");
    if (!regex1.IsMatch(txtMobileNumber.Text.Trim()))
    {
        lblMessage.Show();
    }
}
 
VB.Net
Private Sub OnValidate(sender As Object, e As EventArgs) Handles btnValidate.Click
    lblMessage.Hide()
    Dim regex1 As Regex = New Regex("^([0-9]{10})$")
    If Not regex1.IsMatch(txtMobileNumber.Text.Trim()) Then
        lblMessage.Show()
    End If
End Sub
 
 
Screenshots
Invalid Mobile Number
Mobile Number (Cellphone Number) Validation using RegularExpressions (Regex) in C# and VB.Net
 
Valid Mobile Number
Mobile Number (Cellphone Number) Validation using RegularExpressions (Regex) in C# and VB.Net
 
 
Demo
Mobile Number (Cellphone Number) Validation using RegularExpressions (Regex) in C# and VB.Net
 
 
Downloads