简体   繁体   English

AJAX登录表单的bootbox + jQuery.validator无法正常工作

[英]bootbox + jQuery.validator for AJAX login form not working

I have the following fiddle with my issue. 我对我的问题有以下看法

When I press Sign In on the modal empty form, the validator should mark the fields in error. 当我在模式空表单上按Sign In时,验证器应将字段标记为错误。 Additionally, the form should submit when validation passed. 此外,验证通过时,表单应提交。

There are no errors in console and it does not work. 控制台中没有错误,它不起作用。

This line in my code below seems to not work like it does in this other fiddle : 我下面的代码中的这一行似乎不像其他小提琴那样工作

var form = $("#ajax_login_form");
form.validate();

What did I miss? 我错过了什么? Thanks! 谢谢!

Using jQuery validator, I have the following JS: 使用jQuery验证程序,我有以下JS:

/**
 * Login modal form
 */
function loginModal() {
  var content = $("#login_form").html();
  $("#login_form").detach();

  bootbox.dialog({
    title: "TEST",
    message: content,
    size: 'medium',
    className: 'modal-primary',
    buttons: {
      success: {
        label: "Sign in",
        className: "btn btn-sm btn-primary",
        callback: function() {
          loginValidator();
          var form = $("#ajax_login_form");
          form.validate();
          return false; // keeps dialog open
        }
      }
    },
    onEscape: function() {
      logout();
    }
  });
}

/**
 * Validator for login AJAX form
 */
function loginValidator() {
  $("#ajax_login_form").validate({
    rules: {
      email: {
        required: true,
        email: true
      },
      password: {
        required: true,
        minlength: 8
      }
    },
    messages: {
      email: "Please enter a valid email address",
      password: {
        required: "Please provide a password",
        minlength: $.validator.format("Your password must be at least {0} characters long")
      }
    },
    submitHandler: function(form) {
      alert("submitted!");
      return false;
    }
  });
}

The HTML is: HTML是:

<div id="login_form" style="display:none;">

  <form method="post" action="#" accept-charset="UTF-8" class="form" id="ajax_login_form" name="ajax_login_form">

    <div class="form-group">
      <input class="form-control" id="email" placeholder="Your email" name="email" type="email" />
    </div>

    <div class="form-group">
      <input class="form-control" id="password" placeholder="Your password" name="password" type="password" value="" />
    </div>

    <div class="form-group">
      <input type="checkbox" name="remember" />
      <label for="remember">&nbsp;Remember Me
      </label>
    </div>
  </form>
</div>

Replace 更换

form.validate();

with

form.valid();

This should work as expected (works for me). 这应该按预期工作(对我有用)。

Edit : replace with 编辑 :替换为

form.submit();

if you want to submit the form if it's valid. 如果您想提交有效的表格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM