美文网首页
Bootstrap Validator表单验证插件

Bootstrap Validator表单验证插件

作者: Vincent_Jiang | 来源:发表于2017-05-26 18:23 被阅读1211次

    引入必要文件

    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap-theme.css" rel="stylesheet">
    <link href="https://cdn.bootcss.com/jquery.bootstrapvalidator/0.5.3/css/bootstrapValidator.min.css" rel="stylesheet">
    ...
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="https://cdn.bootcss.com/jquery.bootstrapvalidator/0.5.3/js/bootstrapValidator.min.js"></script>
    

    HTML编写

    <form id="defaultForm" method="post" class="form-horizontal" action="target.php">
        <div class="form-group">
            <label class="col-lg-3 control-label">Full name</label>
            <div class="col-lg-4">
                <input type="text" class="form-control" name="firstName" placeholder="First name" />
            </div>
            <div class="col-lg-4">
                <input type="text" class="form-control" name="lastName" placeholder="Last name" />
            </div>
        </div>
    
        <div class="form-group">
            <label class="col-lg-3 control-label">Username</label>
            <div class="col-lg-5">
                <input type="text" class="form-control" name="username" />
            </div>
        </div>
    
        <div class="form-group">
            <label class="col-lg-3 control-label">Email address</label>
            <div class="col-lg-5">
                <input type="text" class="form-control" name="email" />
            </div>
        </div>
    
        <div class="form-group">
            <label class="col-lg-3 control-label">Password</label>
            <div class="col-lg-5">
                <input type="password" class="form-control" name="password" />
            </div>
        </div>
    
        <div class="form-group">
            <label class="col-lg-3 control-label">Retype password</label>
            <div class="col-lg-5">
                <input type="password" class="form-control" name="confirmPassword" />
            </div>
        </div>
    
        <div class="form-group">
            <label class="col-lg-3 control-label">Gender</label>
            <div class="col-lg-5">
                <div class="radio">
                    <label>
                        <input type="radio" name="gender" value="male" /> Male
                    </label>
                </div>
                <div class="radio">
                    <label>
                        <input type="radio" name="gender" value="female" /> Female
                    </label>
                </div>
                <div class="radio">
                    <label>
                        <input type="radio" name="gender" value="other" /> Other
                    </label>
                </div>
            </div>
        </div>
    
        <div class="form-group">
            <label class="col-lg-3 control-label">Birthday</label>
            <div class="col-lg-5">
                <input type="text" class="form-control" name="birthday" /> (YYYY/MM/DD)
            </div>
        </div>
    
        <div class="form-group">
            <label class="col-lg-3 control-label">Languages</label>
            <div class="col-lg-5">
                <div class="checkbox">
                    <label>
                        <input type="checkbox" name="languages[]" value="english" /> English
                    </label>
                </div>
                <div class="checkbox">
                    <label>
                        <input type="checkbox" name="languages[]" value="french" /> French
                    </label>
                </div>
                <div class="checkbox">
                    <label>
                        <input type="checkbox" name="languages[]" value="german" /> German
                    </label>
                </div>
                <div class="checkbox">
                    <label>
                        <input type="checkbox" name="languages[]" value="russian" /> Russian
                    </label>
                </div>
                <div class="checkbox">
                    <label>
                        <input type="checkbox" name="languages[]" value="other" /> Other
                    </label>
                </div>
            </div>
        </div>
    
        <div class="form-group">
            <label class="col-lg-3 control-label">Programming Languages</label>
            <div class="col-lg-5">
                <div class="checkbox">
                    <label>
                        <input type="checkbox" name="programs[]" value="net" /> .Net
                    </label>
                </div>
                <div class="checkbox">
                    <label>
                        <input type="checkbox" name="programs[]" value="java" /> Java
                    </label>
                </div>
                <div class="checkbox">
                    <label>
                        <input type="checkbox" name="programs[]" value="c" /> C/C++
                    </label>
                </div>
                <div class="checkbox">
                    <label>
                        <input type="checkbox" name="programs[]" value="php" /> PHP
                    </label>
                </div>
                <div class="checkbox">
                    <label>
                        <input type="checkbox" name="programs[]" value="perl" /> Perl
                    </label>
                </div>
                <div class="checkbox">
                    <label>
                        <input type="checkbox" name="programs[]" value="ruby" /> Ruby
                    </label>
                </div>
                <div class="checkbox">
                    <label>
                        <input type="checkbox" name="programs[]" value="python" /> Python
                    </label>
                </div>
                <div class="checkbox">
                    <label>
                        <input type="checkbox" name="programs[]" value="javascript" /> Javascript
                    </label>
                </div>
            </div>
        </div>
    
        <div class="form-group">
            <label class="col-lg-3 control-label" id="captchaOperation"></label>
            <div class="col-lg-2">
                <input type="text" class="form-control" name="captcha" />
            </div>
        </div>
    
        <div class="form-group">
            <div class="col-lg-9 col-lg-offset-3">
                <button type="submit" class="btn btn-primary" name="signup" value="Sign up">Sign up</button>
                <button type="submit" class="btn btn-primary" name="signup2" value="Sign up 2">Sign up 2</button>
                <button type="button" class="btn btn-info" id="validateBtn">Manual validate</button>
                <button type="button" class="btn btn-info" id="resetBtn">Reset form</button>
            </div>
        </div>
    </form>
    

    JS编写

    <script type="text/javascript">
        $(document).ready(function() {
            function randomNumber(min, max) {
                return Math.floor(Math.random() * (max - min + 1) + min);
            };
            $('#captchaOperation').html([randomNumber(1, 100), '+', randomNumber(1, 200), '='].join(' '));
    
            // 验证表单的规则
            $('#defaultForm').bootstrapValidator({
                live: 'enabled',
                message: '输入的值无效',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    firstName: {
                        group: '.col-lg-4',
                        validators: {
                            notEmpty: {
                                message: '不能为空'
                            }
                        }
                    },
                    lastName: {
                        group: '.col-lg-4',
                        validators: {
                            notEmpty: {
                                message: '不能为空'
                            }
                        }
                    },
                    username: {
                        message: '用户名无效',
                        validators: {
                            notEmpty: {
                                message: '请输入用户名'
                            },
                            stringLength: {
                                min: 6,
                                max: 30,
                                message: '用户名长度在 6-30 个字符之间'
                            },
                            regexp: {
                                regexp: /^[a-zA-Z0-9_\.]+$/,
                                message: '用户名包含特殊字符'
                            },
                            remote: {
                                type: 'POST',
                                url: 'remote.php',
                                message: '用户名不可用'
                            },
                            different: {
                                field: 'password, confirmPassword',
                                message: '用户名和密码不能相同'
                            }
                        }
                    },
                    email: {
                        validators: {
                            emailAddress: {
                                message: '请输入正确的邮箱格式'
                            }
                        }
                    },
                    password: {
                        validators: {
                            notEmpty: {
                                message: '请输入密码'
                            },
                            identical: {
                                field: 'confirmPassword',
                                message: '两次密码必须输入一致'
                            },
                            different: {
                                field: 'username',
                                message: '用户名和密码不能相同'
                            }
                        }
                    },
                    confirmPassword: {
                        validators: {
                            notEmpty: {
                                message: '请确认密码'
                            },
                            identical: {
                                field: 'password',
                                message: '两次密码必须输入一致'
                            },
                            different: {
                                field: 'username',
                                message: '用户名和密码不能相同'
                            }
                        }
                    },
                    birthday: {
                        validators: {
                            date: {
                                format: 'YYYY/MM/DD',
                                message: '请输入正确的邮箱格式'
                            }
                        }
                    },
                    gender: {
                        validators: {
                            notEmpty: {
                                message: '请选择一个性别'
                            }
                        }
                    },
                    'languages[]': {
                        validators: {
                            notEmpty: {
                                message: '至少选择一个语言'
                            }
                        }
                    },
                    'programs[]': {
                        validators: {
                            choice: {
                                min: 2,
                                max: 4,
                                message: '请选择 2-4 个编程语言'
                            }
                        }
                    },
                    captcha: {
                        validators: {
                            callback: {
                                message: '错误的答案',
                                callback: function(value, validator) {
                                    var items = $('#captchaOperation').html().split(' '), sum = parseInt(items[0]) + parseInt(items[2]);
                                    return value == sum;
                                }
                            }
                        }
                    }
                }
            }).on('success.form.bv', function(e) {
                // 设定默认提交方式,防止重复提交
                e.preventDefault();
    
                // 提交表单逻辑
                // $.ajax( ... );
            });
    
            // 验证表单
            $('#validateBtn').click(function() {
                $('#defaultForm').bootstrapValidator('validate');
                $("#modal_form").data("bootstrapValidator").isValid(); // 验证表单状态,成功返回true失败返回false
            });
    
            // 重置表单验证
            $('#resetBtn').click(function() {
                $('#defaultForm').data('bootstrapValidator').resetForm(true);
            });
        });
    </script>
    

    相关文章

      网友评论

          本文标题:Bootstrap Validator表单验证插件

          本文链接:https://www.haomeiwen.com/subject/xmvxfxtx.html