name is Name id
提交是提交 ID
帮我转换代码以检查它包含所有字符(没有数字和特殊字符)
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.form_error').hide();
$('#submit').click(function(){
var name = $('#name').val();
if(name== '') {
$('#name').next().show();
return false;
} else {
return true;
}
//ajax call php page
$.post("send.php",
$("#contactform").serialize(), function(response) {
$('#contactform').fadeOut('slow',function(){
});
});
return false;
});
});
</script>
<form action="" method="post" id="contactform">
Name: <input name="name" id="name" type="text"
placeholder="Please enter your name" class="contact-input">
<span class="form_error"> Please enter your valid name</span>
<br><br>
<input type="submit" class="contactform-buttons" id="submit"value="Send" />
</form>
</html>
名称是名称 ID
提交是提交 ID
帮助我转换代码以检查它是否包含所有字符(没有数字和特殊字符)
$(function() {
$('#name').keypress(function (e) {
var regex = new RegExp("^[a-zA-Z]+$");
var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
if (regex.test(str)) {
return true;
}
else
{
e.preventDefault();
alert('Please enter only alphabet');
return false;
}
});
});
替代方法:
$(function() {
$('#name').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();
}
}
});
});