How to only allow numbers in TextBox control in asp.net

Posted by jineesh uvantavida on Saturday, October 14, 2017 Under: Tips & Ideas.
You can simply use regular expression validator as

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1"
        ControlToValidate="TextBox1" runat="server"
        ErrorMessage="Only Numbers allowed"
        ValidationExpression="\d+">

    </asp:RegularExpressionValidator>

Also you can use the below regular expression to make the text-field as 12 digit number to be added.(madatory)

    ^[0-9]{12}$

Also you can make the field as between 10 and 12 digits (mandatory) as below

    ^[0-9]{10-12}$

In : Tips & Ideas. 


Tags: asp.net  textbox control  c#  asp dot net 

Translate This Page

 


Make a free website with Yola