Aug 8, 2012

Regular Expression to Validate a Decimal Number

I had always used to ajaxToolkit MaskedEditExtender for entering decimal value. But I was interested to do the other way round without using ajaxToolkit MaskedEditExtender. Finally I found the solution using Regular Expression. I wanted 4 digit before decimal point and 3 digits after decimal point. Below is how I did it:

<asp:TextBox ID="txtAmount" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator id="RegExVal" runat="server"
ControlToValidate="txtAmount" ValidationExpression="^[0-9]{1,4}(\.[0-9]{0,3})?$"
ErrorMessage="Enter Exact decimal value" />

Happy coding... :)

No comments:

Post a Comment