现在,我有一个像这样的输入字段:
<input class="form-control" type="text"/>
Run Code Online (Sandbox Code Playgroud)
但是它仍然允许输入数字。
我希望输入名称,并且要在字符串包含数字时显示错误消息。我该如何实现?
小智 6
这应该可以帮助您只输入字符:
$(function() {
$('#txtNumeric').keydown(function (e) {
if (e.shiftKey || e.ctrlKey || e.altKey) {
e.preventDefault();
} else {
var key = e.keyCode;
if (!((key == 8) || (key == 32) || (key == 46) || (key >= 35 && key <= 40) || (key >= 65 && key <= 90))) {
e.preventDefault();
}
}
});
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<b>Enter Text:</b>
<input type="text" id="txtNumeric" />
</div>Run Code Online (Sandbox Code Playgroud)