views:

221

answers:

6
<input type="text" id="target" />

How to do it with jQuery?

+1  A: 
$("#target").blur(function(event) {
    event.target.value = event.target.value.replace(/[^0-9]/g, "");
});

This will work as well:

$("#target").keypress(function(event) {
    event.target.value = event.target.value.replace(/[^0-9]/g, "");
});
ChaosPandion
I think it should be something like key up?
Mask
It's not working.
Mask
The problem is the `{^0-9]`, it should be `[^0-9]`.
jtbandes
@jtbandes - I must be going blind.
ChaosPandion
I can still input one letter,but only one.Why?
Mask
Use the keyup event instead of blur or keypress
Justin Johnson
@Justin: The keyup doesn't always trigger (e.g. when using ALT+132 to produce ä), the blur() does.
Huppie
A: 
$('input[type=text].someclass').blur(function(event)
{
   event.target.value = event.target.value.replace(/[^0-9]/g, "");
});
Vegard Larsen
A: 
if (!$.browser.mozilla) {
 if (event.keyCode && (event.keyCode < 48 || event.keyCode > 57))
  event.preventDefault();
}
else {
 if (event.charCode && (event.charCode < 48 || event.charCode > 57))
  event.preventDefault();
}
Mask
The one who did the minus should explain why...
Mike Gleason jr Couturier
A: 
function onlyNumbers(evt) {
    var e = evt;
    if(window.event){ // IE
     var charCode = e.keyCode;
    } else if (e.which) { // Safari 4, Firefox 3.0.4
     var charCode = e.which;
    }
    if (charCode > 31 && (charCode < 48 || charCode > 57))
       return false;
    return true;
}

Source

Soufiane Hassou
+1  A: 

Though a bit more fragile since it uses keycodes... the following would work more intuitive because it makes it completely impossible to enter non-numbers:

$("#target").keydown(function(event) {
    return ( event.keyCode < 32 ||                             // Control characters
             ( event.keyCode >= 48 && event.keyCode <= 57 ) || // Numbers
             event.keyCode == 127 );                           // Delete key
});


Note to add: This is actually not the best way to go... since a (windows) ALT+[num]132 will make it possible to enter ä into the box. It should be combined with the keyup event to ensure that no characters have been entered as well like so, Combined:

$("#target").keydown(function(event) {
    return ( event.keyCode < 32 ||                             // Control characters
             ( event.keyCode >= 48 && event.keyCode <= 57 ) || // Numbers
             event.keyCode == 127 );                           // Delete key
});

$("#target").keyup(function(event) {
    event.target.value = event.target.value.replace(/[^0-9]/g, "");
});

Also, this doesn't work with the num-pad-numbers over here so it definately is more fragile than a simple blur() event.

Huppie
weird, it doesn't seem to work for me, it just stops everything (numbers included)
Soufiane Hassou
@Soufiane: As said, the keydown() / keyCodes are very fragile. See http://unixpapa.com/js/key.html for more information on the details.
Huppie
+1  A: 

There is a nice plugin that will mask your input.

fudgey