ASP.NET C# – Numeric TextBox control validation

There are times when you need to ensure numeric values are entered into a TextBox control.
This can be accomplished using a CompareValidator with the Operator attribute set to "DataTypeCheck" and Type attribute set to the numeric type (Double or Integer).

The sample below allows only double values in the TextBox control.
<asp:TextBox ID="Mileage" runat="server" Columns="8"></asp:TextBox>
...
<asp:CompareValidator ControlToValidate="Mileage" Operator="DataTypeCheck"
 ID="MileageValidator" runat="server" Type="Double"
 Display="Dynamic" ErrorMessage="Please enter a valid mileage amount.">
</asp:CompareValidator>




This entry was posted in Web Development and tagged , , . Bookmark the permalink.