禁用特定字段的jquery.validate功能 [英] Disabling the jquery.validate functionality for specific fields

查看:149
本文介绍了禁用特定字段的jquery.validate功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery.validate在提交给服务器之前验证一些输入字段,客户端。它很棒!这一特定字段组以模态显示,页面上有两个其他模态在不同时间弹出,具体取决于用户点击的内容;但是,我的验证仅在其中一个模态上,即使该模态不在屏幕上,也阻止了我的服务器端提交。

I am using jquery.validate to validate some input fields, client side prior to submitting to the server. It works great! This particular group of fields appears in a modal, there are two other modals on the page that are popped at different times, depending on what the user clicks on; however, my validation is only on one of the modals and even if that modal is not on the screen, it is preventing my server side submission.

以下是用于模态处理的.js中的代码,我想要的是modalChgPass。

Below is my code in .js for the modal processing, the one I want to supress is "modalChgPass".

(function (window) {
    var
   $ = window.jQuery,
   modalEdit = '#modal-preferences-edit',
     modalPreferencesEditOpts = {
         autoOpen: true,
         modal: true,
         resizable: false,
         draggable: false,
         width: '700',
         height: '670',
         dialogClass: 'modal',
         appendTo: 'form',
         close: function () { $(this).dialog('destroy'); }
     };



    modalChgPass = '#modal-password-change',
    modalPasswordChangeOpts = {
        autoOpen: true,
        modal: true,
        resizable: false,
        draggable: false,
        width: '450',
        height: '550',
        dialogClass: 'modal',
        appendTo: 'form',
        close: function () { $(this).dialog('destroy'); }
    };


    modalActive = '#modal-card-activate',
    modalCardActivateOpts = {
         autoOpen: true,
         modal: true,
     resizable: false,
     draggable: false,
     width: '700',
     height: '265',
     dialogClass: 'modal',
     appendTo: 'form',
     close: function() { $(this).dialog('destroy'); }
    };



    $(document).ready(function () {
        $('#btn-modal-password-change').click(function () {
            $(modalChgPass).dialog(modalPasswordChangeOpts);
            $(modalChgPass).dialog('open');
            $('.ui-widget-overlay').click(function () {
                $('.ui-dialog-content').dialog('close');
            });
            return false;
        });

        $('#chg-Password-btn').click(function () {
            $(modalChgPass).dialog(modalPasswordChangeOpts);
            $(modalChgPass).dialog('open');
            $('.ui-widget-overlay').click(function () {
                $('.ui-dialog-content').dialog('close');
            });
            return false;
        });

        $('#btn-modal-card-activate').click(function () {
            $(modalActive).dialog(modalCardActivateOpts);
            $(modalActive).dialog('open');
            $('.ui-widget-overlay').click(function () {
                $('.ui-dialog-content').dialog('close');
            });
            return false;
        });

        $('#btn-modal-preferences-edit').click(function () {
            $(modalEdit).dialog(modalPreferencesEditOpts);
            $(modalEdit).dialog('open');
            $('.ui-widget-overlay').click(function () {
                $('.ui-dialog-content').dialog('close');
            });
            return false;
        });

        });


    window.RCC = window.RCC || {};
})(window);

这里是带有验证的.js:

and here is the .js with the validation:

var $ = jQuery;
    $.validator.addMethod('mypassword', function (value, element) {
        return this.optional(element) || (value.match(/[a-zA-Z]/) && value.match(/[0-9]/));
    },
'Password must contain at least one number.');



$(document).ready(function () {
    $("#form1").validate({
        rules: {
            ChgPass$CurrentPass: {
                required: true
            },
            ChgPass$NewPass: {
                required: true,
                minlength: 8,
                maxlength: 16,
                mypassword: true

            },
            ChgPass$ConfirmPass: {
                equalTo: "#ChgPass_NewPass"
            }
        }

    });
});

如何禁用modalChgPass以允许modalActive和modalEdit处理?
提前感谢您的协助。
问候,

how do I disable the "modalChgPass" to allow "modalActive" and "modalEdit" to process? Thank you in advance for your assistance. Regards,

推荐答案

您无法动态启用和禁用表单上的插件。一旦你调用 .validate()来初始化字段上的规则,你随后调用 .validate()将被忽略。

You cannot dynamically "enable" and "disable" the plugin on the forms. Once you call .validate() to initialize the rules on the fields, you any subsequent call to .validate() will be ignored.

但是, 。规则('添加') .rules('删除')方法将允许您快速动态地添加和删除规则在没有限制的任何字段上。

However, the .rules('add') and .rules('remove') methods will allow you to quickly and dynamically add and remove rules on any of the fields without restriction.

我的简单演示加载了在 .validate()中声明的规则。然后,点击按钮后,通过删除该部分中的所有规则,有效禁用某些字段的验证。

My simple demo loads with the rules as declared within .validate(). Then upon clicking a button, validation is effectively disabled for certain fields by removing all rules from that section.

概念证明DEMO: http://jsfiddle.net/xfSs5/

Proof of Concept DEMO: http://jsfiddle.net/xfSs5/

这篇关于禁用特定字段的jquery.validate功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆