1

jQuery:1.9.1 jQuery 验证:1.11.1

我有一个呈现给用户的用户个人资料页面。当用户对特殊字段(密码或新密码)进行更改时,我想将某些输入字段设为 REQUIRED 和 VALIDATED。但是,规则只起到了一半的作用,并且允许张贴表格。如果我强制执行规则(例如“fullname”和“displayname”),那么它们总是有效的。我不能动态更新验证规则吗?我究竟做错了什么?

<script type="text/javascript">
$(document).ready(
    function()
    {
        function updateRules()
        {
            var mLen = $('#passwordnew').val().length;
            if (mLen > 0)
            {
                $('#passwordold').rules('add', {required: true, minlength: 6});
                $('#passwordold').attr('required', 'true');
                $('#passwordnew').rules('add', {required: true, notEqual: '#passwordold'});
                $('#passwordnew').attr('required', 'true');
                $('#passwordconf').rules('add', {required: true, equalTo: '#passwordnew'});
                $('#passwordconf').attr('required', 'true');
            } else {
                $('#passwordold').rules('remove');
                $('#passwordold').attr('required', 'false');
                $('#passwordnew').rules('remove');
                $('#passwordnew').attr('required', 'false');
                $('#passwordconf').rules('remove');
                $('#passwordconf').attr('required', 'false');
            }
        }

        // Change
        $('#passwordold').change(updateRules);
        $('#passwordnew').change(updateRules);

        // Popover
        $('#profile :input').hover(
            function()
            {
                $(this).popover('show')
            },
            function()
            {
                $(this).popover('hide')
            }
        );

        // Validation
        $('#profile').validate(
            {
                rules:
                    {
                        displayname: {required: false, minlength: 2},
                        emailnew: {required: false, email: true},
                        fullname: {required: true, minlength: 5},
                    },
                messages:
                    {
                        displayname:
                            {
                                minlength: "{{ Lang::line( 'user.profile.displayname.validate.minlength' ) -> get() }}",
                            },
                        emailnew:
                            {
                                email: "{{ Lang::line( 'user.profile.emailnew.validate.email' ) -> get() }}"
                            },
                        fullname:
                            {
                                required: "{{ Lang::line( 'user.profile.fullname.validate.required' ) -> get() }}",
                                minlength: "{{ Lang::line( 'user.profile.fullname.validate.minlength' ) -> get() }}",
                            },
                        passwordold:
                            {
                                required: "{{ Lang::line( 'user.profile.passwordold.validate.required' ) -> get() }}",
                                minlength: "{{ Lang::line( 'user.profile.passwordold.validate.minlength' ) -> get() }}",
                            },
                        passwordnew:
                            {
                                required: "{{ Lang::line( 'user.profile.passwordnew.validate.required' ) -> get() }}",
                                minlength: "{{ Lang::line( 'user.profile.passwordnew.validate.minlength' ) -> get() }}",
                                notEqual: "{{ Lang::line( 'user.profile.passwordnew.validate.notequal' ) -> get() }}",
                            },
                        passwordconf:
                            {
                                required: "{{ Lang::line( 'user.profile.passwordconf.validate.required' ) -> get() }}",
                                equalTo: "{{ Lang::line( 'user.profile.passwordconf.validate.equalto' ) -> get() }}"
                            }
                    },
                errorClass: "help-inline",
                errorElement: "span",
                highlight: function(element, errorClass, validClass)
                {
                    $(element).parents('.control-group').addClass('error');
                },
                unhighlight: function(element, errorClass, validClass)
                {
                    $(element).parents('.control-group').removeClass('error');
                    $(element).parents('.control-group').addClass('success');
                }
            }
        );
    }
);
</script>
4

1 回答 1

0

.validate()是插件的初始化,它应该只在 DOM 准备好时被调用一次。

$(document).ready(function() {

        // Validation INITIALIZATION
        $('#profile').validate({
            // your options and rules
        });

        function updateRules() {
            // your code
        }

        // Change
        $('#passwordold').change(updateRules);
        $('#passwordnew').change(updateRules);

        // Popover
        $('#profile :input').hover(
            function() {
                $(this).popover('show')
            },
            function() {
                $(this).popover('hide')
            }
        );

});
于 2013-04-18T18:41:32.727 回答