Home >Web Front-end >JS Tutorial >jquery validate linkage verification

jquery validate linkage verification

高洛峰
高洛峰Original
2016-11-04 17:03:591663browse

Originally, our project only had ID cards. But now we need to add a Hong Kong, Macao and Taiwan pass for verification.

That is, you have to choose an identity type before choosing.

jquery validate linkage verification

Now it will be changed to this. I have to conduct separate verification of the mainland ID card when the user selects the ID card. If you choose Taiwan, I will verify Taiwan separately. All JS validations on the front end of the project are verified using jquery validate. After struggling for a long time, the original

var join_msg = 1;
jQuery.validator.addMethod("id_card",function(value,element){        var type = $("#id_type").val();
        console.log(type[0].value);        if(!type){
            join_msg = "请选择身份类型";            return  false;
        }        if(type == 1){
            join_msg = "身份证不合法";            var CN = isIdCardNo(value);            return CN;
        }else if( type == 2){

        }
    },function(){        return join_msg;
    }
);

was originally a type obtained separately in the verification. However, I felt that this was unreliable, so I added an extra parameter to the addmethod method.

jQuery.validator.addMethod("id_card",function(value,element,type){        console.log(type[0]);
    },function(){
        
    }
);

规则方面我用的

id_card:{
    required: false,
    id_card: [$("#id_type").val()]
},

But no matter how I pass the parameters to the addmethod method, The type parameter in is always empty. No matter how much I try, I can't get it. In the background, I changed the rules

            id_type:{                required: true
            },            id_card:{                required: false,
                id_card: [id_type]
            },

, which is the id_card. What is actually passed is the id_type above, which is this form element. This is handled in addmethod reception

var join_msg = "请填写正确的证件号码和选择正确的身份类型";
jQuery.validator.addMethod("id_card",function(value,element,type){        var new_type = type[0].value;        console.log(new_type);        if(!new_type){
            join_msg = "请选择身份类型";            return this.optional(element) || false;
        }        if(new_type == 1){
            join_msg = "身份证不合法";            var CN = isIdCardNo(value);            return this.optional(element) || CN;
        }else if(new_type == 2){

        }
    },function(){        return join_msg;
    }
);

This way I can judge whether different ID numbers are correct based on different identity types. The front-end judgment is correct and it is easier for my back-end to process.

See the results:

jquery validate linkage verification

Verified successfully and correctly. And there are different tips.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:JQ common DEMONext article:JQ common DEMO