admin管理员组文章数量:1399188
I want to only allow letters of the alphabet and numbers to be entered in a form field. I have tried many variations using regex, etc. and I cannot disable the space key or any special character keys.
Here is the latest copy of my code:
document.querySelector('#upperCase').addEventListener('keyup', function(e) {
console.log(this.value);
if (e.which == '32') return false;
this.value = this.value.toUpperCase();
});
#upperCase {
text-transform: uppercase;
}
<form>
<input id="upperCase" maxlength="10" name="upperCase" type="text">
</form>
I want to only allow letters of the alphabet and numbers to be entered in a form field. I have tried many variations using regex, etc. and I cannot disable the space key or any special character keys.
Here is the latest copy of my code:
document.querySelector('#upperCase').addEventListener('keyup', function(e) {
console.log(this.value);
if (e.which == '32') return false;
this.value = this.value.toUpperCase();
});
#upperCase {
text-transform: uppercase;
}
<form>
<input id="upperCase" maxlength="10" name="upperCase" type="text">
</form>
Is it possible to only allow letters and number to be keyed into an input text box?
Here is my jsfiddle: https://jsfiddle/bcjg7osr/29/
Share Improve this question edited Sep 6, 2018 at 0:49 Syfer 4,5093 gold badges22 silver badges38 bronze badges asked Sep 5, 2018 at 20:21 jdoejdoe 531 silver badge4 bronze badges4 Answers
Reset to default 3One way you can do it is using the pattern attribute on your input. The only problem with the patter attribute is that it won't disallow spaces until the form is submitted, in which case the submission won't work and the user will be asked to change the input. This will only allow letters and numbers.
<input pattern = "[A-Za-z0-9]" id="upperCase" maxlength="10" name="upperCase"
type="text">
本文标签: javascriptHow to disable or prevent the space key entry on web formStack Overflow
版权声明:本文标题:javascript - How to disable or prevent the space key entry on web form - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744131419a2592199.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论