jquery - form validate rules required depends

て烟熏妆下的殇ゞ 提交于 2019-12-19 10:14:04

问题


Can somebody give me a primer on how to use the validate, rules, required, depends. I have some partial code but not sure how it works.

$("#form2").validate({
    rules: {
        firstname: {
            required: {
                depends: function () {
                    $(this).val($.trim($(this).val()));
                    return true;
                }
            },
            minlength: 2
        },

回答1:


$("#form2").validate({
rules: {
    firstname: {     //This rule applies to the $('#form2 input[name="firstname"]') selector

        required: {  //This can be a true/false boolean, string, or a complex field using depends.

            depends: function () { //depends takes a function pointer that
                                   //returns a value, informing the rule 
                                   //if it is required. In this case, always true.

                $(this).val($.trim($(this).val()));

                return true;
            }
        },

        minlength: 2 // the minimum length of text in the field is 2 characters.
    },


来源:https://stackoverflow.com/questions/10905676/jquery-form-validate-rules-required-depends

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!