我遇到问题“通过”我的表单验证器的验证,它停止并且不会开始处理。当它应该被处理时,它给了我“你需要填写:”
可能出现什么问题?
Jquery的:
$("form").submit(function (e) {
e.preventDefault(); // This will prevent the form submission
var response = "";
var missing = "";
$('#submit-holder').find('input').each(function(){
if ($(this).val() == '') {
missing += $('label[for="' + this.id + '"]').html() + " | ";
empty_fields = true;
missing_fields = true;
}
else if ($(this).val() != '') {
empty_fields = false;
$('.alert').hide();
}
});
response += "* <strong>You need to fill out:</strong> " + missing;
if(!$('#user_terms').is(':checked'))
{
response += "<br>* You need to accept the terms and conditions to continue";
}
var email = $('[name="txtEmail"]').val();
if(IsEmail(email)==false){
valid_email = false;
response += "<br><strong>* The e-mail address you've entered is invalid</strong>";
}
response_message(response);
if(!missing_fields && valid_email !== false && $('#user_terms').is(":checked")) {
$('.alert').hide()
}
});
// Functions:
function IsEmail(email) {
var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if(!regex.test(email)) {
return false;
}else{
return true;
}
}
function response_message(response) {
$('.alert').addClass('alert-danger');
$('.alert').fadeIn();
$('.error_message').html(response);
}
答案 0 :(得分:0)
是一个选择器错误。
以下是工作版本:http://jsfiddle.net/SBYQ5/18/
只需将$('#submit-holder').find('input').each(function () {
替换为$('form').find('input').each(function () {
或 - 或者 - 将submit-holder
添加为表单ID。
此外,您缺少一些var声明。
答案 1 :(得分:0)
更多错误
if (missing) response += "* <strong>You need to fill out:</strong> " + missing;
if (IsEmail(email) == false) {
valid_email = false;
response += "<br><strong>* The e-mail address you've entered is invalid</strong>";
}
else valid_email = true; // missing
response_message(response);
if (!missing_fields && valid_email && $('#user_terms').is(":checked")) {
答案 2 :(得分:0)
当您通过调用该函数检查电子邮件时,您正在检查它是否无效,您将valid_email设置为false。当电子邮件有效时,您不会将其设置为true。
您可以使用
if(IsEmail(email)==false){
valid_email = false;
response += "<br><strong>* The e-mail address you've entered is invalid</strong>";
}
else {
valid_email = true;
}
Or
valid_email = true;
if(IsEmail(email)==false){
valid_email = false;
response += "<br><strong>* The e-mail address you've entered is invalid</strong>";
}