Jquery表单验证 - 如果出现错误,则阻止表单提交

时间:2016-07-20 10:22:34

标签: php jquery validation

我正在使用jquery插件来验证我的表单。如果有不符合验证的条件,我想阻止用户提交表单。这是我在验证中使用的一段代码

      $(function() {
        $("#register-form").validate({
            rules: {
                OMNI_FIRSTNAME: {
                    required: true,
                    minlength: 2,
                    maxlength: 50
                },
                OMNI_LASTNAME: {
                    required: true,
                    minlength: 2,
                    maxlength: 50
                },  
            messages: {
                OMNI_FIRSTNAME: "Please enter first name",
                OMNI_FIRSTNAME: {
                    required: "Please provide firstname",
                    minlength: "Firstname must be at least 2 character long",
                    maxlength: "Firstname reached maximum length of 50 characters long" 
                },
                OMNI_LASTNAME: "Please enter last name",
                OMNI_LASTNAME: {
                    required: "Please provide lastname",
                    minlength: "Lastname must be at least 2 character long",
                    maxlength: "Lastname reached maximum length of 50 characters long"  
                },
            submitHandler: function(form) {
                form.submit();
            }
        });
      });

只要不满足条件,上面的代码就会向我的表单输出消息。我的表单$('#register-form')中有一个提交按钮,用于发布从输入字段(OMNI_FIRSTNAMEOMNI_LASTNAME)输入的数据。我如何将这两个流程整合为一个流程?目前,我仍然可以发布不符合我的jquery验证的数据。

1 个答案:

答案 0 :(得分:2)

在jquery代码中进行以下更改

submitHandler: function(form) {

    form.preventDefault();
    form.submit();
}