Categories
asp.net

Numeric Text Box

We frequently need to ensure only numbers are entered via a Text Box control. Many 3rd party vendors sell contols that do this but that sometimes means buying the suite or that they can’t be styled to match your website. You can do this quite easily using a standard TextBox control and a RegularExpressionValidator.

<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
    ControlToValidate="TextBoxNum" runat="server"
    ErrorMessage="Only Numbers allowed"
    ValidationExpression="^[+-]?(\d*\.)?\d+$" CssClass="smallRed"></asp:RegularExpressionValidator>

If you need posititve integers only use this Regex:
^[1-9]\d*$