Numeric Field in Web Forms

Asked

Viewed 697 times

0

Personal as 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 receive only whole numbers

is Mask in Javascript works only that it deletes values smaller than it as 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 clears the field

  • 1

    You don’t have it in Web Forms, you can use a RegularExpressionValidator, a function Javascript, or even replace with a input normal and setar type=number

  • I created a mask only that it does not accept the minor digits that the mask, I just updated the post, you can help me with this?

  • For me there is no error in your code, are you sure you are selecting the right field? Look at this example I made using the same mask you put in the code and it worked: https://jsfiddle.net/b3mtxceq/1/

  • yes is the right field this, the method you did is by input, I already tried to make the change but the error in the system is like Asp:Textbox and the mask in js is the same as I put in the post

3 answers

0

I managed to resolve it in the following 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>

0

You can try to prevent the user from typing other characters of numbers in the field using Keydown with Jquery or validate on change country.

If your javascript is directly on the page aspx utilize '<%= Feb.Clientid %>' to capture the Clientid of your input field. Else find a way to take the name of the field 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>
<input id="txtNumber1" type="text" placeholder="keydown" maxlength="4" />
<input id="txtNumber2" type="text" placeholder="change" maxlength="4" />

  • the problem is that my field is not as input friend, it is like <Asp:Textbox>

  • I changed the answer with a tip for you. In javascript we use the clientID of the aspx field. ClientID is not always the same name as the field defined on the aspx page.

  • If it works, mark it as an answer. That way, your tagging helps other people with the same question.

-1

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.