3

I'm using this function to only allow numbers in a text input.

$('input').bind('keydown', function(e) {

    var key = e.charCode || e.keyCode || 0;

    return (
         key == 8 ||
         key == 9 ||
         key == 46 ||
         (key >= 37 && key <= 40) ||
         (key >= 48 && key <= 57) ||
         (key >= 96 && key <= 105));
});

How would I also allow copy and paste? I've tried adding keycode 17 for control but it still doesn't work.

Is there something special you have to do for key combinations?

Thanks

11
  • Isn't that first line a jQuery selector? Shouldn't this have, at least, a jQuery tag? Commented Jul 15, 2010 at 16:27
  • or is the tag reserved for questions specific to jQuery? (just curious) Commented Jul 15, 2010 at 16:28
  • @advs89 think about it again (and don't be lazy to read the full question not just the first line...) Commented Jul 15, 2010 at 16:28
  • I don't fully understand: why would you want to prevent people typing in non-numeric characters but then allow them to paste in whatever they want? Commented Jul 15, 2010 at 21:29
  • I imagine the asker wanted the clipboard data validated as well... (i.e. no non-numeric characters) Commented Jul 16, 2010 at 3:18

3 Answers 3

5

You better off with something like:

$('input').bind('keyup', function(e) {
  this.value = this.value.replace(/[^0-9]/g,'');
});

Or you can also use the change event. In this case no matter how the data gets into the field it will be validated (and non numeric input removed). ​

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

2 Comments

+1 for the simplicity and foolproofness of using the change event.
Weird behavior and bad UX when you see incorrect input for a moment and then after some time it disappears.
-1

Keep a record of the last keycode pressed. Since you're using onkeydown, a cmd-v would show up as an event with keycode 224 (cmd) and then an event with keycode 86 (v). If the previous key matches cmd and the latter v, allow it through.

(you would probably check for ctrl for Windows/Linux pasters as well)

1 Comment

There are more ways than that to paste: from the context and edit menus, for example. There's also dragging.
-1

There is input event which is more powerful/flexible than keypress, keydown or change, that reacts on any type of change: program or user type such as key presses or copy/paste events.

// Initial valid input state
let prevValue = ''
let prevSelectionStart = 0

function allowNumbersOnly(event) {
  const input = event.target
  let value = event.target.value

  // Check if value is number
  let isValid = +value == +value

  if (isValid) {
    // preserve input state
    prevValue = value
    prevSelectionStart = input.selectionStart
  } else {
    // restore previous valid input state.
    // we have to fire one more Input event in  order to reset cursor position.
    var resetEvent = new InputEvent('input')
    input.value = prevValue
    input.selectionStart = prevSelectionStart
    input.selectionEnd = prevSelectionStart
    input.dispatchEvent(resetEvent)
  }
}
<input type="text" oninput="allowNumbersOnly(event)">

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.