0

I am trying to allow only numbers [0-9] to be typed in a text box. If an alpha or special character is typed, I do not want it to be shown in the text box. Currently my code is as follows:

$('#TEXTBOX').on("keydown", function(event){
    var keyCode = event.which;
    if(!((keyCode > 47 && keyCode < 58) ||  (keyCode > 95 && keyCode < 106) ||  keyCode == 08)){
        event.preventDefault();
    }
});

I am having a few problems.

  1. This function is still allows special characters [i.e (SHIFT + 1) gives !, (SHIFT + 2) gives @] I do not want these key combinations to allow insert into text box

  2. I am using magic numbers. I would prefer not to use magic numbers and logic but this is the only way I was able to get the input validation to work.... are there any suggestions on other methods?

My main concern is my first problem with the special characters.

j08691
  • 204,283
  • 31
  • 260
  • 272
ola
  • 882
  • 2
  • 12
  • 29
  • I think this question is already been asked many times but I just couldn't find the link – andrex Sep 22 '14 at 14:54
  • You could use [event.shiftKey](http://www.w3schools.com/jsref/event_shiftkey.asp) – Stryner Sep 22 '14 at 14:55
  • 2
    possible duplicate of [How to allow only numeric (0-9) in HTML inputbox using jQuery?](http://stackoverflow.com/questions/995183/how-to-allow-only-numeric-0-9-in-html-inputbox-using-jquery) – andrex Sep 22 '14 at 14:55

4 Answers4

0
$('#TEXTBOX').on("keydown", function(event){
    var keyCode = event.which;
    var charCode = (event.charCode) ? event.charCode : ((event.keyCode) ? event.keyCode: ((event.which) ?   evt.which : 0));
    var char = String.fromCharCode(charCode);
    var re = new RegExp("[0-9]", "i");
    if (!re.test(char)) 
    {
         event.preventDefault(); 
    }
});
0

Use as

$('#TEXTBOX').on("keydown", function(event){
    if(event.shiftKey)
         return false;
    var keyCode = event.which;
    if(!((keyCode > 47 && keyCode < 58) ||  (keyCode > 95 && keyCode < 106) ||  keyCode == 08)){
        event.preventDefault();
    }
});
Amit
  • 15,217
  • 8
  • 46
  • 68
0
$(document).ready(function() {
    $("#txtboxToFilter").keydown(function (e) {
        // Allow: backspace, delete, tab, escape, enter and .
        if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
             // Allow: Ctrl+A
            (e.keyCode == 65 && e.ctrlKey === true) || 
             // Allow: home, end, left, right, down, up
            (e.keyCode >= 35 && e.keyCode <= 40)) {
             // let it happen, don't do anything
                 return;
        }
        // Ensure that it is a number and stop the keypress
        if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
            e.preventDefault();
        }
    });
});
Para
  • 41
  • 2
  • 1
    Please consider adding some explanation on how and why your code snippet does answer OP question. – β.εηοιτ.βε Apr 19 '15 at 10:19
  • As asked by ola, the code above rejects any other combination of keys except for the numbers, decimal and the keys that I've mentioned in the code as comments. I hope that's clear enough. If doubt persists regarding anything else feel free to ask. Just add this jQuery file to your JSP, pHp page, and the result will be there for you to see. – Para May 03 '15 at 16:27
0

Worked For me (Only Numbers are allowed)

            var key = e.charCode || e.keyCode || 0;

            alert(key);

            if (key < 48 || key > 58)
                return false;