-1

If I give a input like he7ck and I want it to ignore anything that isn't letters so it just takes the input as heck how would I do that?

  • 1
    Define "it just takes" in more precise terms – Nino Filiu Jun 18 '20 at 09:18
  • 1
    Does this answer your question? [Remove not alphanumeric characters from string](https://stackoverflow.com/questions/9364400/remove-not-alphanumeric-characters-from-string) – someone Jun 18 '20 at 09:22
  • 1
    Does this answer your question? [How do I make an input field accept only letters in javaScript?](https://stackoverflow.com/questions/23556533/how-do-i-make-an-input-field-accept-only-letters-in-javascript) – Hitesh Tripathi Jun 18 '20 at 09:23

3 Answers3

1

input.oninput = (e) => e.target.value = e.target.value.replace(/[^a-z\s]+/gi, "");
<input id="input"/>
MoloF
  • 251
  • 1
  • 5
0

You can use a simple expression as follows:

document.getElementById("myInput").addEventListener("input",function(){
     var str = this.value;
     this.value = str.replace(/[^a-zA-Z\s]+/g, "")
});
<input id="myInput"/>
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
  • 1
    I'm not sure if it was asked in the question or not but this doesn't support spaces – Sean Jun 18 '20 at 09:25
0

You can use Regex to replace everything that you do not want with nothing "". If you want it to entirely disallow users from entering numbers during input, keep it as it is. Otherwise, if you wanted it to get their full input then apply the restrictions, change the input.oninput function to input.onchange

var input = document.getElementById('input');

input.oninput = function() {
  let data = this.value;
  if (data.match(/[0-9]/g)) {
     console.log(data);
     data = data.replace(/[0-9]/g, "");
     this.value = data;
  }
}
<input id="input">
Sean
  • 767
  • 1
  • 6
  • 19