我想限制所有的符号被输入到我的表单字段在HTML中。停止字符输入窗体字段
这里是我的代码...
<script>
$('#location').keypress(function (e) {
var regex = new RegExp("[^a-zA-Z0-9]+$");
var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
if (regex.test(str)) {
return true;
}
e.preventDefault();
return false;
});
</script>
但是,代码不允许任何空间,甚至使用Delete键。我想要的一切工作,但不希望任何符号(即$#@%^!'“[] {}()等...)
你应该能够只是改变你的正则表达式来调整是什么,是不允许的。如果你想为多个字段添加字段,请将字段添加到您的选择器$(“#location,#otherField,#otherOtherField).keypress ...' – theGleep