6

I have a input field where i only wish users to type numbers

html: <input id="num" type="text" name="page" size="4" value="" />

jquery/ js:

 $("#num").keypress(function (e){
      if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)){
        return false;
      }
});

hope someone can help me.

btw: I'm not interesting in a larger jquery plugin to make the function work. (I have found some jquery-plugins , but there must be som other ways to fix it, with a smaller code)

william
  • 606
  • 3
  • 14
  • 29

12 Answers12

14

Try this:

$("#num").keypress(function (e){
  var charCode = (e.which) ? e.which : e.keyCode;
  if (charCode > 31 && (charCode < 48 || charCode > 57)) {
    return false;
  }
});

Values 48 through 57 represent the digits 0-9.

Dominic Rodger
  • 97,747
  • 36
  • 197
  • 212
  • ya, thats what i'm talking about.. it seems to work just fine! Thx you very much for your help! – william Aug 14 '09 at 07:34
11

Never do this. A user can update a textbox without pressing the key. He can copy paste, drag. some text.

Also this will be irritating to the user.

Just display a label nect to the filed saying that this accepts only numbers. And then

Validate your code at submission

rahul
  • 184,426
  • 49
  • 232
  • 263
  • 1
    Or just validate it on `blur`. – peirix Aug 14 '09 at 07:24
  • Totally agree. Validate on submission, that is so much better. Or use one of these live validation libraries (or roll your own, ofc) that displays an error alongside with the form field. But yeah, not allowing anything but numbers in the input is totally moot. – August Lilleaas Aug 14 '09 at 07:33
  • 4
    I've downvoted because the user has asked a fairly straightforward technical question. I actually agree with your sentiment, but I don't like answers which say the question is wrong. – Dominic Rodger Aug 14 '09 at 07:34
  • 1
    I believe the effect @william is going for is here: http://demos.telerik.com/aspnet-ajax/input/examples/radnumerictextbox/firstlook/defaultcs.aspx If done properly it is unnoticeable to the user and quite effective in providing immediate feedback without having to wait for batch validation to run. – Crescent Fresh Aug 14 '09 at 07:34
  • @crescentfresh - very true, thats exactly what i'm talking about. – william Aug 14 '09 at 07:38
4

Comparing to the current best answer this code is more user-friendly - it allows usage of arrows, backspace, delete and other keys/combinations:

// Ensures that it is a number and stops the key press
$('input[name="number"]').keydown(function(event) {
    if (!(!event.shiftKey //Disallow: any Shift+digit combination
            && !(event.keyCode < 48 || event.keyCode > 57) //Disallow: everything but digits
            || !(event.keyCode < 96 || event.keyCode > 105) //Allow: numeric pad digits
            || event.keyCode == 46 // Allow: delete
            || event.keyCode == 8  // Allow: backspace
            || event.keyCode == 9  // Allow: tab
            || event.keyCode == 27 // Allow: escape
            || (event.keyCode == 65 && (event.ctrlKey === true || event.metaKey === true)) // Allow: Ctrl+A
            || (event.keyCode == 67 && (event.ctrlKey === true || event.metaKey === true)) // Allow: Ctrl+C
            //Uncommenting the next line allows Ctrl+V usage, but requires additional code from you to disallow pasting non-numeric symbols
            //|| (event.keyCode == 86 && (event.ctrlKey === true || event.metaKey === true)) // Allow: Ctrl+Vpasting 
            || (event.keyCode >= 35 && event.keyCode <= 39) // Allow: Home, End
            )) {
        event.preventDefault();
    }
});

Notes: The event.metaKey === true is required for Mac users (thanks RyanM for noticing this). Also if you uncomment Ctrl+V sequence you will need to write additional code for checking pasted text (disallow non-numeric symbols).

Community
  • 1
  • 1
  • please add a reason **why** you think your solution is better – kratenko Jun 05 '12 at 12:32
  • Comparing to the current 'best answer' this code allows usage of arrows, backspace, delete and other keys/combinations. Just try both approaches and you'll feel the difference – Mikhail Berastau Jun 05 '12 at 16:44
  • you should add that to your answer (instead just simply stating: It is better). It would greatly improve your answer and explain what your answer has that the others don't. Answers that give new aspects to earlier answers often get upvotes. Use the `edit` link. – kratenko Jun 05 '12 at 17:42
  • This works great for me. Is it perfect? Are there any cases in which this will break my site? – RyanM Nov 08 '12 at 18:28
  • A few notes: I think you'd want to remove the Ctrl+v part as it allows pasting non-numeric characters, or add more code to disallow pasting non-numeric characters. Also, if you use this, you should change each `event.ctrlKey === true` to `(event.ctrlKey === true || event.metaKey === true)` to accommodate the command key on macs. – RyanM Nov 08 '12 at 18:53
2

Here is my solution:

function InputValidator(input, validationType, validChars) {
if (input === null || input.nodeType !== 1 || input.type !== 'text' && input.type !== 'number')
    throw ('Please specify a valid input');

if (!(InputValidator.ValidationType.hasOwnProperty(validationType) || validationType))
    throw 'Please specify a valid Validation type';

input.InputValidator = this;

input.InputValidator.ValidCodes = [];

input.InputValidator.ValidCodes.Add = function (item) {
    this[this.length] = item;
};

input.InputValidator.ValidCodes.hasValue = function (value, target) {
    var i;
    for (i = 0; i < this.length; i++) {
        if (typeof (target) === 'undefined') {
            if (this[i] === value)
                return true;
        }
        else {
            if (this[i][target] === value)
                return true;
        }
    }

    return false;
};

var commandKeys = {
    'backspace': 8,
    'tab': 9,
    'enter': 13,
    'shift': 16,
    'ctrl': 17,
    'alt': 18,
    'pause/break': 19,
    'caps lock': 20,
    'escape': 27,
    'page up': 33,
    'page down': 34,
    'end': 35,
    'home': 36,
    'left arrow': 37,
    'up arrow': 38,
    'right arrow': 39,
    'down arrow': 40,
    'insert': 45,
    'delete': 46,
    'left window key': 91,
    'right window key': 92,
    'select key': 93,
    /*creates Confusion in IE */
    //'f1': 112,
    //'f2': 113,
    //'f3': 114,
    //'f4': 115,
    //'f5': 116,
    //'f6': 117,
    //'f7': 118,
    //'f8': 119,
    //'f9': 120,
    //'f10': 121,
    //'f11': 122,
    //'f12': 123,
    'num lock': 144,
    'scroll lock': 145,
};

commandKeys.hasValue = function (value) {
    for (var a in this) {
        if (this[a] === value)
            return true;
    }

    return false;
};

function getCharCodes(arrTarget, chars) {
    for (var i = 0; i < chars.length; i++) {
        arrTarget.Add(chars[i].charCodeAt(0));
    }
}

function triggerEvent(name, element) {
    if (document.createEventObject) {
        // dispatch for IE
        var evt = document.createEventObject();
        return element.fireEvent('on' + name, evt)
    }
    else {
        // dispatch for firefox + others
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent(name, true, true); // event type,bubbling,cancelable
        return !element.dispatchEvent(evt);
    }
}

if (validationType == InputValidator.ValidationType.Custom) {
    if (typeof (validChars) === 'undefined')
        throw 'Please add valid characters';

    getCharCodes(input.InputValidator.ValidCodes, validChars);
}
else if (validationType == InputValidator.ValidationType.Decimal) {
    getCharCodes(input.InputValidator.ValidCodes, '0123456789.');
}
else if (validationType == InputValidator.ValidationType.Numeric) {
    getCharCodes(input.InputValidator.ValidCodes, '0123456789');
}

input.InputValidator.ValidateChar = function (c) {
    return this.ValidCodes.hasValue(c.charCodeAt(0));
}

input.InputValidator.ValidateString = function (s) {
    var arr = s.split('');

    for (var i = 0; i < arr.length; i++) {
        if (!this.ValidateChar(arr[i])) {
            arr[i] = '';
        }
    }

    return arr.join('');
}

function bindEvent(el, eventName, eventHandler) {
    if (el.addEventListener) {
        el.addEventListener(eventName, eventHandler, false);
    } else if (el.attachEvent) {
        el.attachEvent('on' + eventName, eventHandler);
    }
}

function getCaretPosition(i) {
    if (!i) return;

    if ('selectionStart' in i) {
        return i.selectionStart;
    }
    else {
        if (document.selection) {
            var sel = document.selection.createRange();
            var selLen = document.selection.createRange().text.length;
            sel.moveStart('character', -i.value.length);
            return sel.text.length - selLen;
        }
    }
}

function setCursor(node, pos) {
    var node = (typeof (node) === "string" || node instanceof String) ? document.getElementById(node) : node;

    if (!node) {
        return false;
    }
    else if (node.createTextRange) {
        var textRange = node.createTextRange();
        textRange.collapse(true);
        textRange.moveEnd(pos);
        textRange.moveStart(pos);
        textRange.select();
        return true;
    } else if (node.setSelectionRange) {
        node.setSelectionRange(pos, pos);
        return true;
    }

    return false;
}

function validateActive() {
    if (input.isActive) {
        var pos = getCaretPosition(input);

        var arr = input.value.split('');

        for (var i = 0; i < arr.length; i++) {
            if (!this.ValidateChar(arr[i])) {
                arr[i] = '';

                if (pos > i)
                    pos--;
            }
        }
        console.log('before : ' + input.value);

        input.value = arr.join('');
        console.log('after : ' + input.value, input);
        setCursor(input, pos);

        setTimeout(validateActive, 10);
    }
}

bindEvent(input, 'keypress', function (e) {
    var evt = e || window.event;
    var charCode = evt.which || evt.keyCode;

    if (!input.InputValidator.ValidCodes.hasValue(charCode) && !commandKeys.hasValue(charCode)) {
        if (evt.preventDefault) {
            evt.preventDefault();
            evt.stopPropagation();
        }
        return false;
    }
});

bindEvent(input, 'keyup', function (e) {
    var evt = e || window.event;
    var charCode = evt.which || evt.keyCode;

    if (!input.InputValidator.ValidCodes.hasValue(charCode) && !commandKeys.hasValue(charCode)) {
        if (evt.preventDefault) {
            evt.preventDefault();
            evt.stopPropagation();
        }
        return false;
    }
});

bindEvent(input, 'change', function (e) {
    var dt = input.value;

    input.value = input.InputValidator.ValidateString(input.value);

    if (input.value !== dt)
        triggerEvent('change', input);
});

bindEvent(input, 'blur', function (e) {
    var dt = input.value;
    input.value = input.InputValidator.ValidateString(input.value);

    input.isActive = false;

    if (input.value !== dt)
        triggerEvent('blur', input);
});

bindEvent(input, 'paste', function (e) {
    var evt = e || window.event;
    var svt = input.value;

    if (evt && evt.clipboardData && evt.clipboardData.getData) {// Webkit - get data from clipboard, put into editdiv, cleanup, then cancel event
        if (/text\/html/.test(evt.clipboardData.types)) {
            var dt = evt.clipboardData.getData('text/html');

            input.value = input.InputValidator.ValidateString(dt);
            if (input.value !== dt)
                triggerEvent('change', input);
        }
        else if (/text\/plain/.test(e.clipboardData.types)) {
            var dt = evt.clipboardData.getData('text/plain');

            input.value = input.InputValidator.ValidateString(dt);
            if (input.value !== dt)
                triggerEvent('change', input);
        }
        else {
            input.value = '';
        }
        waitforpastedata(input, svt);
        if (e.preventDefault) {
            e.stopPropagation();
            e.preventDefault();
        }
        return false;
    }
    else {// Everything else - empty editdiv and allow browser to paste content into it, then cleanup
        input.value = '';
        waitforpastedata(input, svt);
        return true;
    }
});

bindEvent(input, 'select', function (e) {
    var evt = e || window.event;

    if (evt.preventDefault) {
        evt.preventDefault();
        evt.stopPropagation();
    }
    return false;
});

bindEvent(input, 'selectstart', function (e) {
    var evt = e || window.event;

    if (evt.preventDefault) {
        evt.preventDefault();
        evt.stopPropagation();
    }
    return false;
});

/* no need to validate wile active,
   removing F keys fixed IE compatability*/
//bindEvent(input, 'fucus', function (e) {
//    input.isActive = true;
//    validateActive();
//});

//validate current value of the textbox
{
    var dt = input.value;
    input.value = input.InputValidator.ValidateString(input.value);

    //trigger event to indicate value has changed
    if (input.value !== dt)
        triggerEvent('change', input);
}

function waitforpastedata(elem, savedcontent) {
    if (elem.value !== '') {
        var dt = input.value;
        elem.value = elem.InputValidator.ValidateString(elem.value);

        if (input.value !== dt)
            triggerEvent('change', input);
    }
    else {
        var that = {
            e: elem,
            s: savedcontent
        }
        that.callself = function () {
            waitforpastedata(that.e, that.s)
        }
        setTimeout(that.callself, 10);
    }
}
}

InputValidator.ValidationType = new (function (types) {
    for (var i = 0; i < types.length; i++) {
        this[types[i]] = types[i];
    }
})(['Numeric', 'Custom', 'Decimal']);

To apply it to an input, do the following :

new InputValidator(document.getElementById('txtValidate'), InputValidator.ValidationType.Decimal);/* Numeric or Custom */

If you specify Custom as the validation type you have to specify the valid characters. eg :

new InputValidator(document.getElementById('txtValidate'), InputValidator.ValidationType.Custom,'1234abc');
Frans
  • 66
  • 2
2

Here's my code:

$("Input.onlyNumbersInput").live('input', function (event) {

            $(this).val($(this).val().replace(/[^0-9]/g, ''));

});

Using "live('input')" event will make the function replace any character that is not a digit, which the user input, WITHOUT the user see the character.

If you use "onkeyup" event, the user will see the character for few miliseconds, which is not cool.

felippe
  • 493
  • 6
  • 7
2

This works for me:

$(document).ready(function () {
  $(".onlyNumbersInput").on('keyup keydown blur', function (event) {
    $(this).val($(this).val().replace(/[^0-9]/g, ''));
  });
});

It's based on @luizfelippe answer, updated for jquery 1.7+

This code allows paste and will remove any non numeric character in realtime.

Antonio Max
  • 8,627
  • 6
  • 43
  • 42
0

I think you forgot putting e.preventDefault(); before return.

thSoft
  • 21,755
  • 5
  • 88
  • 103
Thinker
  • 14,234
  • 9
  • 40
  • 55
0

This is how I do it:

jQuery( '#input').keyup(function() {

    var value = jQuery(this).val();

    var newValue = value.replace(/\D/g,''); //remove letters
    newValue =  parseInt(newValue);
    jQuery(this).val(newValue);

    value = jQuery(this).val();

    //just as safety, if some weird character is in the input
    if(  isNaN (value) || value  == "" ){
        jQuery(this).val(0);
    }
}

The keyup event seems just fine for me to do this, I've also tried keypress and keydown.

0

My JQuery Solution.

const validKeyCodes = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57]
    $('#your_input').on('keypress', function(e) {
      return validKeyCodes.indexOf(e.keyCode) > -1
    })
oldmayn
  • 161
  • 2
  • 3
  • 14
0

text input:

<input id="number" type="text" />

and script:

var number = $('#number');
number.on('input', function () {
    $(this).val(getOnlyNumbers($(this).val()));
});

// this function return only numbers as string or ''
function getOnlyNumbers(value) {
                let arr = value.toString().split('');
                let number = [];

                arr.forEach(function (elem) {
                    let charCode = elem.charCodeAt(0);

                    if (charCode > 31 && (charCode < 48 || charCode > 57)) {

                    } else {
                        number.push(elem);
                    }
                });

                return parseInt(number.join('')) || '';
            }
  • Hey, thank you for your answer. It is highly recommended to provide an explanation how the solution solves the issue instead of just providing some code. – KillerX Feb 19 '20 at 10:28
0

KeyCodes or charCodes is not the best way to do this if you use numbers above letters in your laptop and don't have normal keyboard connected. And if you know these numbers above letters, starting with key `(~) , were used to add specific symbols of different nations language. So only numbers will become numbers and letters. For example: key 1(!) - in Lithuanian language is ą . So better way is to use this

var newValue = value.replace(/\D/g,'');
This taken from answer above.
-1

Try this Script:

var str = 5.5;
str = str.toString();
var errnum = [];
var onlyNumbers = [0,1,2,3,4,5,6,7,8,9,"0","1","2","3","4","5","6","7","8","9"];

for(i = 0; i < str.length; i++) { 
  if(jQuery.inArray(str[i],onlyNumbers) == -1){
       errnum.push(str[i]);
  }
}

if(errnum.length >= 1){
     console.log('not numeric character found!');
}
MarmiK
  • 5,639
  • 6
  • 40
  • 49
  • Welcome to Stack Overflow! Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. – IKavanagh Sep 16 '15 at 10:31