我的表单中有这个jQuery验证控件。它正在运作:
$("#webform-client-form-45").submit(function(){
if($("#edit-submitted-name-surname").val() == "" || $("#edit-submitted-name-surname").val() == "Name Surname"){
return false;
}else if($("#edit-submitted-telephone").val() == "" || $("#edit-submitted-telephone").val() == "Telephone"){
return false;
}else if($("#edit-submitted-e-posta").val() == "" || $("#edit-submitted-e-posta").val() == "E-MAIL"){
return false;
}else{ if($("#edit-submitted-messages").text() == "" || $("#edit-submitted-messages").text() == "Message"){
return false;
}else
return true;
}
});
但是当用户在此字段中将字段留空或写错时,我想显示警告。例如,“请输入电子邮件地址”“
我该怎么做?
答案 0 :(得分:1)
使用提醒框:
if($("#edit-submitted-name-surname").val() == "" || $("#edit-submitted-name-surname").val() == "Name Surname")
{
alert("Please input your name");
return false;
}
使用div:
// HTML
<input type = "text" name = "edit-submitted-name-surname" id = "edit-submitted-name-surname" />
<div class = "error" id = "name_error">Please input your name</div>
// CSS
.error{ display: none; color: red;}
// jquery的
Put this at the top of the function: $('.error').hide();
if($("#edit-submitted-name-surname").val() == "" || $("#edit-submitted-name-surname").val() == "Name Surname")
{
$('#name_error').show();
return false;
}