我有一个简单的名/姓形式。提交时,我想检查是否只有字母已被使用,如果没有,则显示错误div。如果输入包含字母之外的任何字母,则显示div
例如:
if ('#input-32') {
/* contains anything other than letters */
$("#error").show();
}
我有一个简单的名/姓形式。提交时,我想检查是否只有字母已被使用,如果没有,则显示错误div。如果输入包含字母之外的任何字母,则显示div
例如:
if ('#input-32') {
/* contains anything other than letters */
$("#error").show();
}
下应该显示错误元素,如果输入包含除信件以外的其他任何东西:
if ($('#input-32').val().match(/[^a-zA-Z]/g)) {
/* contains anything other than letters */
$("#error").show();
}
您可以使用正则表达式 - Read More
const input = document.querySelector('#firstname');
input.addEventListener('input', function(e) {
\t const val = e.target.value;
\t const isLetter = /^[a-zA-Z]+$/.test(val);
\t console.log(isLetter);
// if(isLetter){ ... }
})
<input type="text" id="firstname">
$(document).ready(function(){
$("#error").hide();
$("#nameField").on("change", function(){
var nameSub = $('#nameField').val();
if(/^[a-zA-Z]+$/.test(nameSub)){
$("#error").hide();
}
else{
$("#error").show();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="name" id="nameField"/>
<div id="error">error</div>
你尝试过自己吗? – Granny
请研究jQuery验证器 – user1428716
也请记住:有些名称中包含非字母。另外考虑检查ÄÜÜ或其他特殊字符。 – cloned