jQuery Validate: input just required if another input is filled

后端 未结 1 1708
滥情空心
滥情空心 2021-01-24 14:30

I am using jQuery Validate plugin to validate a \"configuration page\". I have a lot of inputs (like Name, Phone, Email...) and for last, \"change password\" section.

I

相关标签:
1条回答
  • 2021-01-24 15:12

    Try

    var $cp = $('#current_password'),
        $np = $('#new_password'),
        $cnp = $('#confirm_new_password');
    

    then

    {
        rules: {
            current_password: {
                required: function () {
                    return $np.val().length > 0 || $cnp.val().length > 0
                }
            },
            new_password: {
                required: function () {
                    return $cp.val().length > 0 || $cnp.val().length > 0
                }
            },
            confirm_new_password: {
                required: function () {
                    return $cp.val().length > 0 || $np.val().length > 0
                }
            }
        },
    }
    

    Demo: Fiddle

    0 讨论(0)
提交回复
热议问题