0

I am trying to restrict an input text to numbers only by checking it in a setInterval and removing the chars that are not numbers. Now so far this works relatively alright, however I am not able to remove "special symbols". For example if I input the Spanish accent symbol ' or symbols like ˇ that should be over a char, it resets the field and the value is not assigned again despite the fact that the symbol gets removed from the string (if I log the string, I can see it is).

Example:

  1. Typing 123a => 'a' removed, input contains 123
  2. Typing 123ˇ => input contains an empty string, despite the fact that the 'text' string contains 123 and has a length of 3.
  3. Typing 123 and pasting ˇ after it => ˇ removed, input contains 123

This seems like it has something to do with the fact that the special symbols are not ...self-standing (?) and need to be over a char that should be inputted next. Some ideas how to solve it? Thank you.

I have the following HTML:

<body>
    <input type="text" id="input_field">
</body>

And the JS:

$("#input_field").focus(function(e) {
            console.log("got focus"+$(this).val());
            var obj = this;
            intID = setInterval(function() {restrictNumbers(obj)}, 10);
        });

        $("#input_field").blur(function(e) {
            console.log("got blur")
            clearInterval(intID);
        });

        function restrictNumbers(field) {
            var text = $(field).val();
            var caretPos;
            var modified = false;
            if(text.length > 0) {
                for(var i=0; i<text.length; i++) {
                    if(isNaN(text.charAt(i))) {
                        modified = true;
                        caretPos = field.selectionStart - 1;
                        text = text.replace(text.charAt(i), "");
                        i--;
                    }
                }
                $(field).val(text);
                if(modified) {
                    modified = false;
                    field.setSelectionRange(caretPos, caretPos);
                }
            }
        }

JSfiddle example: http://jsfiddle.net/AvMZ5/

Fygo
  • 4,555
  • 6
  • 33
  • 47

1 Answers1

0

You can use this:-

jQuery.fn.ForceNumericOnly =
function()
{
    return this.each(function()
    {
        $(this).keydown(function(e)
        {
            var key = e.charCode || e.keyCode || 0;
            // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
            // home, end, period, and numpad decimal
            return (
                key == 8 || 
                key == 9 ||
                key == 46 ||
                key == 110 ||
                key == 190 ||
                (key >= 35 && key <= 40) ||
                (key >= 48 && key <= 57) ||
                (key >= 96 && key <= 105));
        });
    });
};

You can then attach it to your control by doing:

$("#yourTextBoxName").ForceNumericOnly();

Source:- https://gist.github.com/wholypantalones/3083362

Vivek Sadh
  • 4,230
  • 3
  • 32
  • 49
  • Thanks for this, but it doesn't allow any keyboard shortcuts (and it is pretty crucial). – Fygo Jul 08 '13 at 17:53