天天看点

bootstrapValidator 表单验证

$(function () {

        $('form').bootstrapValidator({          

            message: 'This value is not valid',

            feedbackIcons: {

                valid: 'glyphicon glyphicon-ok',

                invalid: 'glyphicon glyphicon-remove',

                validating: 'glyphicon glyphicon-refresh'

            },

            fields: {

                linkman: {

                    message: '用户名验证失败',

                    validators: {

                        notEmpty: {

                            message: '不能为空'

                        },

                    }

                },

                tel: {

                    validators: {

                        notEmpty: {

                            message: '电话号码不能为空'

                        },

                        regexp: {

                            regexp: /^1\d{10}$/,

                            message: '手机号码不正确,请重新输入'

                        }

                    }

                },

                email: {

                    validators: {

                        notEmpty: {

                            message: '邮箱不能为空'

                        },

                        regexp: {

                            regexp: /^\[email protected]\w+(\.[a-zA-Z]{2,3}){1,2}$/,

                            message: 'Email格式不正确,例如[email protected]m'

                        }

                    }

                },

                talker_info: {

                    validators: {

                        notEmpty: {

                            message: '不能为空'

                        },

                        stringLength: {

                            min: 0,

                            max: 300,

                            message: '0/300个字符'

                        }

                    }

                },

                summary: {

                    validators: {

                        notEmpty: {

                            message: '不能为空'

                        },

                        stringLength: {

                            min: 0,

                            max: 300,

                            message: '0/300个字符'

                        }

                    }

                },

                field: {

                    validators: {

                        notEmpty: {

                            message: '不能为空' 

                        },

                        stringLength: {

                            min: 0,

                            max: 300,

                            message: '0/100个字符'

                        }

                    }

                },

                account: {

                    validators: {

                        notEmpty: {

                            message: '不能为空' 

                        },

                        stringLength: {

                        }

                    }

                },

               theme: {

                    validators: {

                        notEmpty: {

                            message: '不能为空' 

                        },

                        stringLength: {

                            min: 0,

                            max: 50,

                            message: '0/50个字符'

                        }

                    }

                },

                act_time: {

                    validators: {

                        notEmpty: {

                            message: '不能为空' 

                        }

                    }

                }, 

                address: {

                    validators: {

                        notEmpty: {

                            message: '不能为空' 

                        }

                    }

                },

                title: {

                    validators: {

                        notEmpty: {

                            message: '不能为空' 

                        }

                    }

                },               

            }

        })

    function GetQueryString(name) {

        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");

        var r = window.location.search.substr(1).match(reg);

        if(r != null) return unescape(r[2]);

        return null;

    }

    // 调用方法

    var ssid = GetQueryString("ssid");    

    $(".btn").click(function(){

        var flag = $('#defaultForm').data("bootstrapValidator").isValid();//校验合格

        if(flag){

            var $form = $('#defaultForm');

                    $.ajax({

                    type:"post",

                    url:"https://dev-api.yiketalks.com/v4/talker/talkerApply",

                    data:$("#defaultForm").serialize(),    

                    headers: {

                        "Accept": "text/plain; charset=utf-8",

        //                "Content-Type": "text/plain; charset=utf-8",

                        "ssid": ssid

                    },  

                    async:true,    

                    success: function (result) {

                        $(".concentBox").css("display","none");

                        $('.jsrz_main_check').css("display","block");

                }

                });

            }

});

    });