In this short code snippet article I will share the following Regular Expressions (Regex) for validating decimal numbers in TextBox.
1. Regular Expressions (Regex) to allow both decimals as wells as integers numbers.
2. Regular Expressions (Regex) to allow any decimal number starting from 1 upto N decimal places.
 
 
Regular Expressions (Regex) to allow both decimals as wells as integers
Regular Expression (Regex)
((\d+)((\.\d{1,2})?))$
 
Explanation
This regular expression looks for any number of digits. And if dot character is added it will look for decimal places upto 2 decimals.
 
Example
<asp:TextBox ID="TextBox1" runat="server" />
<br />
<asp:RegularExpressionValidator ID="Regex1" runat="server" ValidationExpression="((\d+)((\.\d{1,2})?))$"
ErrorMessage="Please enter valid integer or decimal number with 2 decimal places."
ControlToValidate="TextBox1" />
 
 
Regular Expressions (Regex) to allow any decimal number starting from 1 upto N decimal places
Regular Expression (Regex)
((\d+)+(\.\d+))$
 
Explanation
This regular expression looks for any number of digits followed by a dot character and ending with minimum one decimal place.
 
Example
<asp:TextBox ID="TextBox2" runat="server" />
<br />
<asp:RegularExpressionValidator ID="Regex2" runat="server" ValidationExpression="((\d+)+(\.\d+))$"
ErrorMessage="Please enter valid decimal number with any decimal places." ControlToValidate="TextBox2" />
 
 
Screenshots
Invalid values
Regular Expression (Regex) which allow both decimals as well as integers and Regular Expression (Regex) for any decimal number
 
Valid values
Regular Expression (Regex) which allow both decimals as well as integers and Regular Expression (Regex) for any decimal number
 
 
Demo
 
 
Downloads