2017-10-14 83 views

回答

1

您可以明确说明您接受的字符HTML input pattern Attribute

如果硬要上阻止特定的字符,你可以使用以下方法:?

document.getElementById("explicit-block-txt").onkeypress = function(e) { 
 
    var chr = String.fromCharCode(e.which); 
 
    if ("></\"".indexOf(chr) >= 0) 
 
     return false; 
 
};
<input type='text' id='explicit-block-txt' value='' onpaste="return false"/>

1

为什么不使用HTML5

<input type="text" pattern="[^()/><\][\\\x22,;|]+"> 
+0

这种方式必须在表单中使用。 – selmansamet

1

您可以使用正则表达式。

document.getElementById("input").onkeypress = function(e) { 
 
    /^[a-zA-Z0-9]+$/.test(this.value) // return true or false 
 
};
<input type="text" id="input">