Accept only number by TextBox using Jquery

July 12, 2015 0 Comments

You can say, how to apply restriction on input using Jquery. Here, meaning of restriction is, TextBox accept only numbers. Everyone know that TextBox control take character,string, numeric and special character as a input. But in some application we need only single feature like, it accept only number. So, the first question raise in my mind, how to do this, what is the logic behind this.
Logic behind this:
  1. First to get the key code like number 8 == backspace, some following keycode available here 
  2.  Now, match the keycode with the input character, if match then input character are not allowed.
Here are simple example of it:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
    <title>Accept only numbers</title>
<script src="scripts/jquery-1.10.2.js"></script>
$(function () {
$('#Text1').keydown(function (er) {

if (er.altKey || er.ctrlKey || er.shiftKey) {
else {

var key = er.keyCode;
if (!((key == 8) || (key == 46) || (key >= 35 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105))) {






    <form id="form1" runat="server">
<input id="Text1" type="text" />
Now code generate the following output:

Accept only number by TextBox using Jquery
Here are some related article:

Jacob Lefore

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google