0

我在“通过”表单验证器的验证时遇到问题,它停止并且不会开始处理。当它应该被处理它给我“你需要填写:”

有什么问题?

菲德

查询:

  $("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);
}       
4

3 回答 3

0

这是一个选择器错误。

这是工作版本:http: //jsfiddle.net/SBYQ5/18/

只需替换$('#submit-holder').find('input').each(function () {$('form').find('input').each(function () {

或者 - 或者 - 添加submit-holder为表单 ID。

此外,您缺少一些 var 声明。

于 2013-08-22T12:20:48.473 回答
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")) {
于 2013-08-22T12:28:50.173 回答
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>";
}
于 2013-08-22T13:08:51.573 回答