使用Dialog进行jQuery验证

时间:2010-10-28 12:55:35

标签: jquery jquery-validate jquery-ui-dialog

我使用错误消息的对话框可以很好地运行jQuery验证。我遇到的问题是,当我单击提交,并且所有字段都填写正确时,我会在表单提交之前弹出一个空白对话框。我似乎无法弄清楚如何阻止这种情况发生。将onSubmit设置为false将不起作用,因为它根本不会验证。有任何想法吗?

这是包含我所有jQuery的脚本标记:

    <script type="text/javascript">
    $(document).ready(function () {
        $("#tabs").tabs();

        $("#DateOfBirth").datepicker({ changeYear: true, changeMonth: true });

        $("#InquiryForm").validate({
            submitHandler: function (form) {
                //form.submit();
            },
            rules: {
                FirstName: { required: true },
                LastName: { required: true },
                Phonenumber: { required: true },
                Email: { required: true, email: true },
                Address: { required: true },
                Country: { required: true },
                Zip: { required: true },
                CourseDeliveryTime: { required: true },
                ProgramType: { required: true },
                ProgramofInterest: { required: true },
                ProgramType: { required: true },
                DateOfBirth: { required: true, date: true }
            },
            messages: {
                FirstName: { required: 'First Name is required.<br/>' },
                LastName: { required: 'Last Name is required.<br/>' },
                Phonenumber: { required: 'Phone Number is required.<br/>' },
                Email: { required: 'E-Mail is required.<br/>', email: 'Please enter a valid e-mail address' },
                Address: { required: 'Mailing Address is required.<br/>' },
                Country: { required: 'Country is required.<br/>' },
                Zip: { required: 'Zip Code is required.<br/>' },
                CourseDeliveryTime: { required: 'Please tell us when you would like to attend class.<br/>' },
                ProgramType: { required: 'Preferred Location is required.<br/>' },
                ProgramofInterest: { required: 'Intended Academic Program is required.<br/>' },
                ProgramType: { required: 'Preferred Location is required.<br/>' },
                DateOfBirth: { required: 'Date of Birth is required.', date: 'Please enter a valid date i.e. 01/01/1999' }
            },
            errorPlacement: function (error, element) {
                error.appendTo($("#dialog"));

            },
            showErrors: function (errorMap, errorList) {
                this.defaultShowErrors();
                $("#dialog").dialog('open');
            },
            errorContainer: "#dialog",
            errorLabelContainer: "#dialog ul",
            wrapper: "li", debug: true,
            onfocusout: false,
            onclick: false
        });
        $("#dialog").dialog({
            autoOpen: false,
            modal: true,
            close: function (event, ui) {
                $("#dialog").html("");
            },
            title: 'Please fix the following:',
            resizable: false,
            width: 300
        });
        $("#privacyPolicy").dialog({
            autoOpen: false,
            modal: true,
            title: 'Privacy Policy',
            resizable: false,
            height: 210,
            width: 500
        });
        $("#linkPrivacyPolicy").click(function () {
            $("#privacyPolicy").dialog("open");
            return false;
        });
    });
</script>

1 个答案:

答案 0 :(得分:0)

您可以将开放代码移至invalidHandler,以便只有在 错误时才会运行,方法是将其替换为:

showErrors: function (errorMap, errorList) {
  this.defaultShowErrors();
  $("#dialog").dialog('open');
},

有了这个:

invalidHandler: function() {
  $("#dialog").dialog('open');
},