0

I found this code through Stack Overflow to restrict users from putting numbers in a textbox, however it only works in Chrome and IE. Anyone know of any cross browser code to do this? Note: we've already added the global attribute and it didn't work at all, this was the only one that fully worked in Chrome and IE.

<input type="text" onkeyup="this.value = this.value.replace(/[^a-z]/, '')" />
7
  • Here's the code: <input type="text" onkeyup="this.value = this.value.replace(/[^a-z]/, '')" /> Commented Oct 11, 2017 at 19:39
  • It appears you are doing this via Javascript as per your code example. Are you using a Javascript library like Angular or jQuery in your project? What level of browser support are you looking for? Is this purely for display or for validation or both? Commented Oct 11, 2017 at 19:42
  • have you tried using oninput instead? Commented Oct 11, 2017 at 19:47
  • This is working for me in Chrome, Firefox, Edge, IE and Safari. Commented Oct 11, 2017 at 19:58
  • What browsers are you testing in, What versions? Commented Oct 11, 2017 at 19:58

1 Answer 1

1

You want to catch onkeydown that is when the character gets inserted, not on onkeyup. You should also instead of removing the number, just prevent it from getting inserted with event.preventDefault()

<p>
  <input type="text" onkeydown="event.key.match(/\d/) && event.preventDefault()" />
</p>

One thing I would recommend is removing the code from the html and putting it in a function so it is reusable like this:

// Wait till the dom is loaded
document.addEventListener('DOMContentLoaded', function(e) {
  // Add the event to each input that has `data-type="number"`
  document.querySelectorAll('[data-type=number]').forEach(function(input) {
    // Add the event to the input
    input.addEventListener('keydown', number)
  })
})

function number(event) {
  event.key.match(/\d/) && event.preventDefault()
}
<p>
  <input type="text" data-type="number" />
</p>

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

5 Comments

Maybe change the arrow function in the listener to a normal function in case they aren't using a transpiler for their JS?
querySelector works in IE8/9 I believe whereas the arrow functions won't.
Yeah I looked queryselector up on caniuse.com, it doesn't have full support on ie 8 but does on ie 9. => has no ie support but is supported on edge
Good advice, except input type="number" will only allow numbers rather than prevent numbers. Also beware of unexpected behaviors with input type="number" if you go that route for anything. For instance, leading 0's will get stripped and "e" will be allowed since we only get valid/actual numbers from that type of input.
haha you are right, by the time I got to that one I was thinking they wanted numbers instead of removing them. I removed that example.

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.