Javascript 表单验证
我正在创建一个基本的表单验证器,但由于某些原因,如果有人能够提供帮助,它就不会启动else语句 我确信这是一件愚蠢的事情,但我的印象是,点击它将运行通过条件,如果所有失败,返回错误;将返回到起始位置Javascript 表单验证,javascript,forms,validation,Javascript,Forms,Validation,我正在创建一个基本的表单验证器,但由于某些原因,如果有人能够提供帮助,它就不会启动else语句 我确信这是一件愚蠢的事情,但我的印象是,点击它将运行通过条件,如果所有失败,返回错误;将返回到起始位置 $('.get-results').click(function(e) { e.preventDefault(); var get_results = $('.get-results'), input_h8_val = $('.input_h8
$('.get-results').click(function(e) {
e.preventDefault();
var get_results = $('.get-results'),
input_h8_val = $('.input_h8_val'),
input_h10_val = $('.input_h10_val'),
input_h12_val = $('.input_h12_val'),
input_h14_val = $('.input_h14_val'),
input_h16_val = $('.input_h16_val'),
input_h20_val = $('.input_h20_val'),
input_h21_val = $('.input_h21_val'),
input_h23_val = $('.input_h23_val');
if (input_h8_val.val() == 0) {
$(input_h8_val).next('.error').show();
} else if (input_h10_val.val() == 0) {
$(input_h10_val).next('.error').show();
} else if (input_h12_val.val() == 0) {
$(input_h12_val).next('.error').show();
} else if (input_h14_val.val() == 0) {
$(input_h14_val).next('.error').show();
} else if (input_h16_val.val() == 0) {
$(input_h16_val).next('.error').show();
} else if (input_h20_val.val() == 0) {
$(input_h20_val).next('.error').show();
} else if (input_h21_val.val() == 0) {
$(input_h21_val).next('.error').show();
} else if (input_h23_val.val() == 0) {
$(input_h23_val).next('.error').show();
}
else {
console.log("worked");
$('#first-wrapper').hide();
$('.predicted').addClass('active');
$('.predicted').addClass('no-left-radius');
$('.orders').addClass('no-right-radius');
$('#second-wrapper').show();
$('#third-wrapper').hide();
do_calculations();
}
});
提供的代码适合我,但您必须记住以下几点: 1-如果用户在某个地方填写“0”(默认情况下为“在线订单的百分比是多少?”),验证将失败 2-
next()
只查找紧跟在兄弟姐妹之后的。因此,在某些情况下,触发时不会显示错误。(输入元素后面有另一个span
)
解决此问题的方法:
$(element).parent().find(".error").show();
表单验证示例
姓名:
电子邮件地址:
电话:
请不要给我打电话
我们能帮你做什么?
客户服务
问题:
评论
咨询
其他
评论:
函数ValidateContactForm()
{
var name=document.ContactForm.name;
var email=document.ContactForm.email;
var phone=document.ContactForm.phone;
var nocall=document.ContactForm.DoNotCall;
var what=document.ContactForm.Subject;
var comment=document.ContactForm.comment;
如果(name.value==“”)
{
window.alert(“请输入您的姓名”);
name.focus();
返回false;
}
如果(email.value==“”)
{
window.alert(“请输入有效的电子邮件地址”);
email.focus();
返回false;
}
if(email.value.indexOf(“@”,0)<0)
{
window.alert(“请输入有效的电子邮件地址”);
email.focus();
返回false;
}
if(email.value.indexOf(“.”,0)<0)
{
window.alert(“请输入有效的电子邮件地址”);
email.focus();
返回false;
}
如果((nocall.checked==false)&&(phone.value==“”)
{
窗口提示(“请输入您的电话号码”);
phone.focus();
返回false;
}
if(what.selectedIndex<1)
{
警惕(“请告诉我们如何帮助您。”);
什么。焦点();
返回false;
}
如果(comment.value==“”)
{
警告(“请提供详细说明或评论”);
comment.focus();
返回false;
}
返回true;
}
功能启用禁用(chkbx)
{
if(chkbx.checked==true)
{
document.ContactForm.Telephone.disabled=true;
}
其他的
{
document.ContactForm.Telephone.disabled=假;
}
}
您可以发布一个包含表单HTML的JSFIDLE吗?您是否尝试为您的输入值添加console.log()
,以查看提交的确切值?通过这种方式,您可以正确调试代码。我已经尝试了console.log,条件似乎很好,但它从未到达else语句tho。在这里,它起作用了:我更改了“在线订单的百分比”的值,然后单击按钮,它在控制台中显示“已工作”。请记住,如果用户填写“0”,它不会接受该值。感谢您的帮助。家长选择器也有帮助。为了让阅读问题及其答案的人受益,请详细说明您在代码示例中所做的哪些工作可以解决原始问题?非常感谢。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Form Validation Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width" intial-scale="1">
<link rel="stylesheet" type="text/css" href="G:/bootstrap-3.3.6-dist/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<form method="post" action="mailto:Frank@cohowinery.com"
name="ContactForm" onsubmit="return ValidateContactForm();">
<p>Name: <input type="text" size="30" name="Name"></p>
<p>E-mail Address: <input type="text" size="30" name="Email"></p>
<p>Telephone: <input type="number" size="30" name="Telephone"><br>
<input type="checkbox" name="DoNotCall"
onclick="EnableDisable(this);"> Please do not call me.</p>
<p>What can we help you with?
<select type="text" value="" name="Subject">
<option> </option>
<option>Customer Service</option>
<option>Question</option>
<option>Comment</option>
<option>Consultation</option>
<option>Other</option>
</select></p>
<p>Comments: <textarea cols="55" name="Comment"> </textarea></p>
<p><input type="submit" value="Send" name="submit">
<input type="reset" value="Reset" name="reset"></p>
</form>
</body>
<script>
function ValidateContactForm()
{
var name = document.ContactForm.Name;
var email = document.ContactForm.Email;
var phone = document.ContactForm.Telephone;
var nocall = document.ContactForm.DoNotCall;
var what = document.ContactForm.Subject;
var comment = document.ContactForm.Comment;
if (name.value == "")
{
window.alert("Please enter your name.");
name.focus();
return false;
}
if (email.value == "")
{
window.alert("Please enter a valid e-mail address.");
email.focus();
return false;
}
if (email.value.indexOf("@", 0) < 0)
{
window.alert("Please enter a valid e-mail address.");
email.focus();
return false;
}
if (email.value.indexOf(".", 0) < 0)
{
window.alert("Please enter a valid e-mail address.");
email.focus();
return false;
}
if ((nocall.checked == false) && (phone.value == ""))
{
window.alert("Please enter your telephone number.");
phone.focus();
return false;
}
if (what.selectedIndex < 1)
{
alert("Please tell us how we can help you.");
what.focus();
return false;
}
if (comment.value == "")
{
window.alert("Please provide a detailed description or comment.");
comment.focus();
return false;
}
return true;
}
function EnableDisable(chkbx)
{
if(chkbx.checked == true)
{
document.ContactForm.Telephone.disabled = true;
}
else
{
document.ContactForm.Telephone.disabled = false;
}
}
</script>
</html>