8

I was using Jquery Numeric plugin but I found that is not working on FireFox 3.6 on Osx (does not allow pasting).

I'm searching a Jquery plugin or Javascript snippet that allows just Numeric Text on an Input Text field.
I have the following requirements:

  1. Should allow just numeric text
  2. Should NOT allow punctuation (.,)
  3. Should NOT allow dashed text (-)
  4. Should allow pasting just for numeric text
  5. Multibrowser
  6. Multiplatform
4

3 Answers 3

7

DEMO: http://so.devilmaycode.it/allowing-just-numeric-text-on-input-text-field/

jQuery.fn.onlyDigits = function() {
    var k;
    // little trick just in case you want use this:
    $('<span></span>').insertAfter(this);
    var $dText = $(this).next('span').hide();
    // Really cross-browser key event handler
    function Key(e) {
        if (!e.which && ((e.charCode ||
        e.charCode === 0) ? e.charCode: e.keyCode)) {
        e.which = e.charCode || e.keyCode;
        } return e.which; }
    return $(this).each(function() {
        $(this).keydown(function(e) {
            k = Key(e);
            return (
            // Allow CTRL+V , backspace, tab, delete, arrows,
            // numbers and keypad numbers ONLY
            ( k == 86 && e.ctrlKey ) || (k == 86 && e.metaKey) || k == 8 || k == 9 || k == 46 || (k >= 37 && k <= 40 && k !== 32 ) || (k >= 48 && k <= 57) || (k >= 96 && k <= 105));
        }).keyup(function(e) {
            var value = this.value.replace(/\s+/,'-');
            // Check if pasted content is Number
            if (isNaN(value)) {
                // re-add stored digits if CTRL+V have non digits chars
                $(this).val($dText.text());
            } else { // store digits only of easy access
                $dText.empty().append(value);
            }
        });
    });
};

USAGE:

$("#onlydigits").onlyDigits();

NOTE:

thanks to Insider for pointing out the past (cmd+v) for MAC


key event handler REFERENCES:

Sign up to request clarification or add additional context in comments.

9 Comments

i can't test it bro! i don't have a MAC and I'll never have one! ;-) can you tell me what exactly doesn't work!? grazie! ;-)
It does not allow pasting.Grazie a te :).
ok, updated one more time, now it use e.ctrlKey instead of key code 17, not sure, but it should work as expected! let me know!
hey bro, no problem, at this point, i want make it to work, but i need a little help! can you alert() the k var and see what number it give to you!?
I see 224.Sorry for the delay but i was in vacation.
|
5

Thanks to dev-null-dweller i have resolved with this script:

jQuery('#input').bind('keyup blur',function(){ 
   jQuery(this).val( jQuery(this).val().replace(/[^0-9]/g,'') ); }
);

1 Comment

Came here to share almost the same thing; except that I used "\d" instead of [0-9]. :P
0

Thanks systempuntoout.. I've fixed this with following script

$('#input').keyup( function() {
  var $this = $(this);
  $this.val($this.val().replace(/[^\d.]/g, ''));     
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.