Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net Textbox Textmode Number, allow numbers only

Tags:

c#

asp.net

I would just like to know if there is a way in ASP.NET to allow only numbers in textboxes with textmode="number"

when I use this:

<asp:TextBox runat="server" TextMode="Number" ID="TextBoxDuration" Width="250"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="TextBoxDuration" runat="server" ErrorMessage="Dieses Feld darf nicht leer sein" /><br />
<asp:RegularExpressionValidator runat="server" ControlToValidate="TextBoxDuration" validationexpression="((\d+)((\.\d{1})?))$" ErrorMessage="Nur Zahlen" />

users can still input the char e and +, -

I don't like to use normal Textbox with the same Regularexpressionvalidator (which actually would work)

example:

enter image description here

like image 469
Robin Schambach Avatar asked Dec 19 '22 16:12

Robin Schambach


1 Answers

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}$
like image 148
Jineesh Uvantavida Avatar answered Dec 30 '22 22:12

Jineesh Uvantavida