<input type="text"/>
I assume that there is some kind of regular expression to solve this problem, but I could not find on the Internet which one.. I will be very grateful if you help =')
Updated: the main question is how to prevent the user from entering the character "`" in the Input field. I don't understand hot to do this.
CodePudding user response:
You can add an eventlistener to the input field and check for any charachter, that you don't want for each keystroke.
I made a quick demo: https://codesandbox.io/s/vigilant-leaf-h2syxv?file=/index.html:115-160
let item = document.querySelector(".name-input");
item.addEventListener("keypress", (e) => {
if (e.key === "~" || e.key === "`") {
e.preventDefault();
}
});
<input type="text" />
CodePudding user response:
So you should start by making a list of the special characters that you would to prevent users from using them, i assume that your list contains : [~,',",(,),`]. Then you can use this :
let name = document.querySelector('.name-input')
let regex = "[`'"()~]"
if ( name.match(regex) ){
console.log('There was a match');
} else {
console.log('NO match');
}