asp.net Textbox Textmode Number, allow numbers only

匿名 (未验证) 提交于 2019-12-03 01:34:02

问题:

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:

回答1:

You can use jQuery like this

Number : <input type="text" name="quantity" id="quantity" />&nbsp;<span id="errmsg"></span>      <script>         $(document).ready(function () {             //called when key is pressed in textbox             $("#quantity").keypress(function (e) {                 //if the letter is not digit then display error and don't type anything                 if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {                     //display error message                     $("#errmsg").html("Digits Only").show().fadeOut("slow");                     return false;                 }             });         });      </script> 


回答2:

You can use RegularExpressionValidator for this. below is the sample code:

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

above TextBox only allowed integer to be entered because in RegularExpressionValidator has field called ValidationExpression, which validate the TextBox. However, you can modify as per your requirement.

You can see more example in MVC and Jquery here.



回答3:

you can make a Javascript function into your Textbox.

Try <asp:TextBox ID="txtNumero" Text="" onkeydown="return jsDecimals(event);" Width="75px" runat="server"></asp:TextBox>

Then

function jsDecimals(e) {      var evt = (e) ? e : window.event;     var key = (evt.keyCode) ? evt.keyCode : evt.which;     if (key != null) {         key = parseInt(key, 10);         if ((key < 48 || key > 57) && (key < 96 || key > 105)) {             //Here is where respond with 0 o 1.             if (!jsIsUserFriendlyChar(key, "Decimals")) {                 return false;             }         }         else {             if (evt.shiftKey) {                 return false;             }         }     }     return true; } 


回答4:

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}$ 


回答5:

REGEX

^[0-9]*$   <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="tbAccount"     ErrorMessage="Please Enter Only Numbers" ForeColor="Red" ValidationExpression="^\d+$">        </asp:RegularExpressionValidator> 

IF YOU DON'T WANT USER TO TYPE OTHER CHARACTER ON KEY EVENT THEN

$('#TEXTBOXID').keypress(function (e) {     var a = [];     var k = e.which;      for (i = 48; i < 58; i++)     a.push(i);      // allow a max of 1 decimal point to be entered      if (!(a.indexOf(k) >= 0)) e.preventDefault();   }); 


回答6:

you can use filter textbox extender. it will allows only number. there is no negative numbers or e,+,- keys

<asp:FilteredTextBoxExtender ID="TextBox1_FilteredTextBoxExtender" runat="server" Enabled="True" TargetControlID="txtDay"  FilterType="Numbers"> </asp:FilteredTextBoxExtender> 


回答7:

You can also use asp.net field validate like this

<asp:TextBox runat="server" CssClass="form-control"  ID="txtNumero"  />                                                 <asp:RegularExpressionValidator                                                     ID="txtNumero"                                                     ControlToValidate="EmailTextBox"                                                     Text="(Invalid number)"                                                     ForeColor="Red"                                                     ValidationExpression="^\d+$"                                                     runat="server" />  


回答8:

Adding Type="number" will work.

<asp:TextBox ID="txtQty"  type="number" runat="server"></asp:TextBox> 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!