Numerical Field in Web Forms

0

Personally I define a field that is in text in webforms for numeric, as an example.

<asp:TextBox ID="feb" runat="server" BorderColor="#999999" BorderStyle="Solid" MaxLength="4" TabIndex="93" Width="90px" ></asp:TextBox>

I need this field to only receive integers

This mask in JavaScript works only as it erases smaller values as an example

jQuery("#ctl00_ctl00_ContentPrinc_ContentPlaceHolder1_feb").mask("9999");

If I type 45 in the field and the mask is "9999" it does not accept and clean the field

    
asked by anonymous 27.02.2018 / 18:44

3 answers

0

You can try to prevent the user from entering other characters than numbers in the field using KeyDown with JQuery or validate in the change field.

If your javascript is directly on the aspx page, use ' feb.ClientID%' to capture the ClientID of your input field. Otherwise, find a way to get the field name for the event declaration. Ex:

var txtNumber = '<%= feb.ClientID %>';

$("#txtNumber1").on('keydown', function (e) {
      return ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105) 
      || (e.keyCode == 8 || e.keyCode == 46));
});
        
$("#txtNumber2").on('change', function () {
    $(this).val(ApenasNumeros($(this).val()));
});
        
 function ApenasNumeros(valor) {
      valor = valor.replace(/[^0-9]+/g, "");
      return valor;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputid="txtNumber1" type="text" placeholder="keydown" maxlength="4" />
<input id="txtNumber2" type="text" placeholder="change" maxlength="4" />
    
27.02.2018 / 19:06
0

I was able to resolve this in a personal way.

function somenteNumeros(num) {
    var er = /[^0-9]/;
    er.lastIndex = 0;
    var campo = num;
    if (er.test(campo.value)) {
        campo.value = "";
    }
}

<asp:TextBox ID="feb" runat="server" BorderColor="#999999" BorderStyle="Solid" MaxLength="4" onkeyup="somenteNumeros(this);" TabIndex="93" Width="90px" ></asp:TextBox>
    
27.02.2018 / 20:00
-1

You can add the EntryType="inteiro" field to TextBox , so it only accepts whole numbers.

    
11.01.2019 / 17:52