简体   繁体   English

如何防止用户在输入字段中输入字符“`”?

[英]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.

相关问题 如何防止用户在 ReactJS 的输入表单字段中输入空格? - How to prevent user from entering spaces in an input form field in ReactJS? 如何防止用户在输入字段中输入无效字符 - How to prevent users from entering invalid characters inside an input field 如何防止用户在Vue 3数字输入中输入低于1的值 - How to prevent user from entering value below 1 in Vue 3 number input 如果一定数量的数字已经具有值,则阻止用户在输入字段中输入数字 - Prevent the user from entering a number in the input field if a certain number of them already have a value javascript:如何防止用户在文本框中输入=? - javascript : How to prevent user from entering = in textbox? 如何防止用户输入小数? - How to prevent user from entering decimals? 防止用户在输入字段中输入大于2位数字的值 - Prevent users from entering a value greater than 2 digits in an input field 如何防止可选输入字段的占位符值在离开数据库时进入数据库? - How to prevent an optional input field's placeholder value from entering into the database on leaving it? Knockoutjs可观察订阅(循环)-防止用户输入错误的输入 - Knockoutjs Observable Subscription (loop) - Prevent user from entering wrong input 如果前一个字段为空,如何防止用户输入值? - How to prevent user entering value if previous field is empty?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM