[英]How to prevent the user from entering the character "`" in the Input field?
<input class="name-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.
我不明白这样做很热。
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我做了一个快速演示: 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 class="name-input" type="text" />
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');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.