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 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 upto 2 decimal places.
 
 
Regular Expressions (Regex) to match number with decimal precision of exact 1 decimal place
Regular Expression (Regex)
 ((\d+)(\.\d{1}))$
 
Explanation
This regular expression looks for any number of digits followed by a dot character and ending with one digit character.
 
Example
<asp:TextBox ID="TextBox1" runat="server" />
<br />
<asp:RegularExpressionValidator ID="Regex1" runat="server" ValidationExpression="((\d+)(\.\d{1}))$"
ErrorMessage="Please enter valid decimal number with 1 decimal place." ControlToValidate="TextBox1" />
 
 
Regular Expressions (Regex) to match number with decimal precision of exact 2 decimal places
Regular Expression (Regex)
 ((\d+)(\.\d{2}))$
 
Explanation
This regular expression looks for any number of digits followed by a dot character and ending with two digit characters.
 
Example
<asp:TextBox ID="TextBox2" runat="server" />
<br />
<asp:RegularExpressionValidator ID="Regex2" runat="server" ValidationExpression="((\d+)(\.\d{2}))$"
ErrorMessage="Please enter valid decimal number with 2 decimal places." ControlToValidate="TextBox2" />
 
 
Regular Expressions (Regex) to match number with decimal precision of exact 2 decimal places
Regular Expression (Regex)
 ((\d+)(\.\d{1,2}))$
 
Explanation
This regular expression looks for any number of digits followed by a dot character and ending with minimum one digit or maximum two digits.
 
Example
<asp:TextBox ID="TextBox3" runat="server" />
<br />
<asp:RegularExpressionValidator ID="Regex3" runat="server" ValidationExpression="((\d+)(\.\d{1,2}))$"
ErrorMessage="Please enter valid decimal number upto 2 decimal places." ControlToValidate="TextBox3" />
 
 
Screenshots
Invalid values
Regular Expression (Regex) to match number with exact 1 decimal place, exact 2 decimal places, or upto 2 decimal places
 
Valid values
Regular Expression (Regex) to match number with exact 1 decimal place, exact 2 decimal places, or upto 2 decimal places
 
 
Demo
 
 
Downloads