1

Well i have made this jquery code

$(document).ready(function(){
    $(".graduate").keyup(function(e){
        if(e.keyCode!=8 && (e.keyCode<48 && e.keyCode>57)){
        //some code here
        };
    });
});

I want input ignore write anything except integer numbers..

I found this: but i don't know how to convert it into jquery code.. http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onkeypress

function noNumbers(e)
{
var keynum;
var keychar;
var numcheck;

if(window.event) // IE
    {
    keynum = e.keyCode;
    }
else if(e.which) // Netscape/Firefox/Opera
    {
    keynum = e.which;
    }
keychar = String.fromCharCode(keynum);
numcheck = /\d/;
return numcheck.test(keychar);
}

The above code doesn't allow me to use backspace.. Any help?

Chris P
  • 2,059
  • 4
  • 34
  • 68
  • Another approach: check the value set in the input and if is not a number (regexp, isNaN, etc..), return the string in the input to its previous content. – Alfabravo Dec 27 '11 at 15:20
  • 2
    @Alfabravo what about this? http://stackoverflow.com/questions/891696/jquery-what-is-the-best-way-to-restrict-number-only-input-for-textboxes-all – Chris P Dec 27 '11 at 15:23
  • Don't ask me, try it and tell us!! :D (didn't know the plugin, I did such validation by hand some years ago :P ) – Alfabravo Dec 27 '11 at 15:25

3 Answers3

2
$(".graduate")    
.bind('keydown', function(e) {
    var
    editingKeys = {
        '8'   : 'delete',
        '9'   : 'tab',
        '46'  : 'canc',
        '37'  : 'leftarrow',
        '39'  : 'rightarrow',
    },

    key = e.which || e.keycode,
    keynum = (key > 47) && (key < 58),
    keypad = (key > 95) && (key < 106);

    if (!keynum && !keypad) {
        return (key in editingKeys);
    }
});
Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
0

jQuery will "always" give you the keycode in e.which. You can safely use e.which != 8 in your if statement inside the keyup handler. i.e..

$(".graduate").keyup(function(e){
    if(e.which!=8 && (e.keyCode<48 && e.keyCode>57)){
        //some code here
    }
});
techfoobar
  • 65,616
  • 14
  • 114
  • 135
  • this is not the problem.. The problem is what i must right until `//some code` to ignore everything excpet integer numbers -- return false doesn't work – Chris P Dec 27 '11 at 15:38
0

Use regexp: http://jsfiddle.net/DSvkS/

$(document).ready(function(){
    $(".graduate").bind('keyup', function(event) {
        this.value = this.value.replace(/\D/g, "");
    });
});

\Dmeans anything but 0-9.

or support . and , http://jsfiddle.net/DSvkS/4/

$(document).ready(function(){
    $(".graduate").bind('keyup', function(event) {
        this.value = this.value.replace(/[^0-9\.,]/g, "");
    });
});

[^...] means anything but ...:

0-9 is 0,1,2,3,4,5,6,7,8,9

\. is . We have to escape the dot using \ as the dot means match anything.

, is ,

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
  • The problem is when i put some char into input box.. I see it and the disapeared.. That's not good – Chris P Dec 27 '11 at 15:39