0
我有一个简单的工作ajax联系表单(大部分大声笑),我有php验证工作,并发送我的电子邮件。我的问题是jquery部分,我想验证并根据输入错误的类型显示错误消息。现在看来它总是对我的第一条if语句返回true(总是说输入字段中没有值)。我的代码在哪里出错?简单的jquery输入名称表单验证
if (!$.trim(("#contact-name").value).length) {
console.log("Error: Required Name");
$("#contact-name").next().text('Your name is required.');
}
else {
var re = /^[a-zA-Z ]+$/;
var is_name = re.test("#contact-name").val();
if (is_name){
error_free = true;
console.log("Success: Name is Valid");
}
else {
$("#contact-name").next().text('Your name cannot contain special characters');
error_free = false;
console.log("Error: Name cannot contain special characters");
}
}
你确定'$(“#contact-name”).value'仍然是正确的?它应该是'$(“#contact-name”)。val()' –
@KingKing是的,你是对的。谢谢。 – Chang
太棒了,谢谢! – cheesecake28