JQuery Validation Plug In强制双击提交表单

时间:2011-03-12 22:31:43

标签: jquery forms jquery-plugins validation

我必须在表单上单击两次“提交”按钮才能提交。如何制作,以便用户只需点击提交?但是,我不想onfocusout也不想启用onkeyup。我只想通过单击“提交”按钮验证我的表单。

    $(document).ready(function() {
        $.validator.setDefaults({
          onfocusout: false,
          onkeyup: false
        });

        $.validator.addMethod("username", function(value, element) {
           return this.optional(element) || /^[a-z]+$/i.test(value);
            }, "Your username may only contain letters");

        var validator = $("#registerForm").validate({
          showErrors: function(errorMap, errorList) {
            $("#registerForm").find("input").each(function() {
                $(this).removeClass("error");
            });
            $("#errorcontainer").html("");
            if(errorList.length) {
              $("#errorcontainer").html(errorList[0]['message']);
              $(errorList[0]['element']).addClass("error");
              $('#errorcontainer').css('display','none');
              $('#errorcontainer').fadeIn(400);
            }
            else {
              $('#errorcontainer').css('display','none');
            }
          },

          rules: {
            username: {
              required: true,
              rangelength: [3,15],
              remote: "usernamevalidation.php"
            },
            password: {
              required: true,
              rangelength: [5,20]
            },
            passwordconfirm: {
              required: true,
              equalTo: "#password"
            },
            email: {
              required: true,
              email: true,
              remote: "emailvalidation.php"
            }
          },

          messages: {
            username: {
              required: "You must enter a username.",
              rangelength: "Your username must be between {0} and {1} letters.",
              remote: "Your desired username is already in use."
            },
            password: {
              required: "You must enter a password.",
              rangelength: "Your password must be between {0} and {1} letters."
            },
            passwordconfirm: {
              required: "Your passwords must match.",
              equalTo: "Your passwords must match."
            },
            email: {
              required: "You must enter an email address.",
              email: "Your email address is not valid.",
              remote: "This email is already in use."
            }
          }
        });
      });

4 个答案:

答案 0 :(得分:2)

您好我使用了jquery validate附带的shubmitHandler

submitHandler: function(form) { form.submit(); return false });

我希望它可以帮到你

答案 1 :(得分:1)

以下是为我解决的问题:

仅供参考,我也使用jQuery UI(v1.8.11),但没有关于上述内容的问题:

希望它有所帮助!

答案 2 :(得分:0)

像这样使用遥控器

remote:{
url: "emailvalidation.php",
async:false
}

这里“async:false”允许在第一次点击时提交表单, 希望对你有所帮助。

答案 3 :(得分:0)

我遇到了同样的问题并通过在我的验证中添加以下内容来解决它:

$("#myform").validate({
  onfocusout: false
});

https://jqueryvalidation.org/validate/

了解详情