Dailycode.info

Short solution for short problems

Disallow non numeric characters in an asp.net textbox

A very simple javascript that will block the entry of non numeric characters:

function isNumberKey(evt) {

    var charCode = (evt.which) ? evt.which : event.keyCode

    if (charCode > 31 && (charCode < 48 || charCode > 57))

        return false;

 

    return true;

}

Simply put this on the onkeydown event:

<dx:ASPxTextBox runat="server" ID="txtTaskSortOrder" Width="50px" Value='<%# Eval("TaskSortOrder")%>'

MaxLength="5" onkeypress="return isNumberKey(event);">

</dx:ASPxTextBox>

 

If the user enters non numeric characters, the entry will be withhold and not allowed in the textbox.