当前位置: 首页 > 工具软件 > jqValidate > 使用案例 >

jq validate remote有关值处理问题记录

鲜于谦
2023-12-01

   $("#form1").validate({
            onkeyup: false,
            rules: {
                unitName: {
                    required: true,
                    remote: {
                        url: '/user/check?type=groupName',
                        type: 'Get',
                        data: {
                            name: function () {
                                return $("#groupname").val();
                            }
                        },
                        dataFilter: function (data) {
                         //这里想过很多次为啥直接返回data.exist 会出现对,
                        //错都显示错误提示,也找了很多博客,但都没相关的,
                        //我这里就只发现要转换成Json对象才有用
                            let res = JSON.parse(data)
                            return res.exist ? false : true
                        }
                    }
                },
                userName: {
                    required: true,
                    remote: {
                        url: '/user/check?type=userName',
                        type: 'Get',
                        data: {
                            name: function () {
                                return $("#username").val();
                            }
                        },
                        dataFilter: function (data) {
                       
                            let res = JSON.parse(data) 
                            return res.exist ? false : true
                        }
                    }
                },
                phonenumber: {
                    required: true,
                    minlength: 11,
                    maxlength: 11
                },
                phoneCode: {
                    required: "",
                    minlength: 6
                },
                password: {
                    required: true,
                    minlength: 6,
                    maxlength: 20
                },
                confirm_password: {
                    required: true,
                    minlength: 6,
                    maxlength: 20,
                    equalTo: "#password"
                },
                email: {
                    required: true,
                    email: true
                },
                emailCode: {
                    required: true,
                    minlength: 6
                }
            },
            messages: {
                unitName: {
                    required: "请输入组织名称",
                    remote: '该组织机构已存在'
                },
                userName: {
                    required: "请输入用户名",
                    remote: '该用户名已存在'
                },
                phonenumber: {
                    required: "请输入手机号",
                    minlength: "不足十一位"
                },
                phoneCode: {
                    required: "",
                    minlength: ""
                },
                password: {
                    required: "请输入密码",
                    minlength: "密码长度不能小于6个字符",
                    maxlength: "密码长度不能小于20个字符"
                },
                confirm_password: {
                    required: "请输入确认密码",
                    minlength: "密码长度不能小于6个字符",
                    maxlength: "密码长度不能小于20个字符",
                    equalTo: "两次密码输入不一致"
                },
                email: "请输入一个正确的邮箱",
                emailCode: {
                    required: "",
                    minlength: ""
                }
            }
        });

    });
    $.validator.setDefaults({
        submitHandler: function (from) {
            var param = $("#form1").serialize();
            $.ajax({
                url: "register",
                type: "post",
                dataType: "json",
                data: param,
                success: function (result) {
                    if (result.code == 0) {
                        window.location.href = "/user/step2";
                    } else {
                        alert(result.msg);
                    }
                }
            });
        },
        invalidHandler: function (form, validator) {  //不通过回调
            return false;
        }
    });
 类似资料: