In this article I will explain with an example, how to implement decimal number validation 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. Regular Expressions (Regex) to match number with decimal precision of exact 1 decimal place.
2. Regular Expressions (Regex) to match number with decimal precision of exact 2 decimal places.
3. Regular Expressions (Regex) to match number with decimal precision of up to 2 decimal places.
 
 
Form Design
The following Form consists of three TextBoxes, six Labels and a Button controls.
Regular Expression (Regex) to implement decimal number validation 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 Number with decimal precision of exact 1 decimal place
Regular Expression (Regex)
^((\d+)(\.\d{1}))$
 
Explanation
Above Regular Expression validates for any number of digits followed by a dot character and ending with one digit 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.
C#
private void OnValidate1(object sender, EventArgs e)
{
    lblMessage1.Hide();
    Regex regex1 = new Regex(@"^((\d+)(\.\d{1}))$");
    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("^((\d+)(\.\d{1}))$")
    If Not regex1.IsMatch(txt1.Text.Trim()) Then
        lblMessage1.Show()
    End If
End Sub
 
 
Validating Number with decimal precision of exact 2 decimal places
Regular Expression (Regex)
^((\d+)(\.\d{2}))$
 
Explanation
Above Regular Expression validates any number of digits followed by a dot character and ending with two digit characters.
 
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 OnValidate2(object sender, EventArgs e)
{
    lblMessage2.Hide();
    Regex regex2 = new Regex(@"^((\d+)(\.\d{2}))");
    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("^((\d+)(\.\d{2}))")
    If Not regex2.IsMatch(txt2.Text.Trim()) Then
        lblMessage2.Show()
    End If
End Sub
 
 
Validating Number with decimal precision up to 2 decimal places
Regular Expression (Regex)
^((\d+)(\.\d{1,2}))$
 
Explanation
Above Regular Expression validates any number of digits followed by dot character and ending with minimum one digit or maximum two 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)
{
    lblMessage3.Hide();
    Regex regex3 = new Regex(@"^((\d+)(\.\d{1,2}))$");
    if (!regex3.IsMatch(txt3.Text.Trim()))
    {
        lblMessage3.Show();
    }
}
 
VB.Net
Private Sub OnValidate(sender As Object, e As EventArgs) Handles btnValidate3.Click
    lblMessage3.Hide()
    Dim regex3 As Regex = New Regex("^((\d+)(\.\d{1,2}))$")
    If Not regex3.IsMatch(txt3.Text.Trim()) Then
        lblMessage3.Show()
    End If
End Sub
 
 
Screenshots
Invalid values
Regular Expression (Regex) to implement decimal number validation in C# and VB.Net
 
Valid values
Regular Expression (Regex) to implement decimal number validation in C# and VB.Net
 
 
Demo
Regular Expression (Regex) to implement decimal number validation in C# and VB.Net
 
 
Downloads