0

I am using this code to allow only digits to type in textbox but now I want to allow . too. I modified this code but instead of allowing . it allows hyphen -.

function isNumberKeyDotAllowed(evt) {
    var charCode = evt.which ? evt.which : evt.keyCode;
    if (charCode == 46 || (48 <= charCode && charCode <= 57)) {
        return true;
    }
    else {
        return false;
    }
}

button:

<asp:TextBox runat="server" ID="txtBoxAreaSqft" OnTextChanged="txtBoxAreaSqft_TextChanged"
                                                    PlaceHolder="Enter Digits Only" onkeypress="return isNumberKeyDotAllowed(event)" Enabled="true" AutoPostBack="true" CssClass="form-control"></asp:TextBox>
Cuckoo
  • 177
  • 1
  • 2
  • 10

2 Answers2

0

Just try Once in html onKeydown instead of onKeypress might help as i thing so its updating value mismatch

mayur
  • 3,558
  • 23
  • 37
0

Try this:

  <asp:TextBox ID="txt" runat="server" onkeydown="return isNumOrDot(event)" />

<script>
function isNumOrDot(e){
    var nums = [48,49,50,51,52,53,54,55,56,57,190/*dot*/,
                96,97,98,99,100,101,102,103,104,105,/*Num keys*/
                110 /*num dot*/];
    return nums.indexOf(e.which)>-1;
}
</script>

onkeydown="return... is the key.

Alex Kudryashev
  • 9,120
  • 3
  • 27
  • 36