欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于Bootstrap+jQuery.validate实现Form表单验证

程序员文章站 2023-02-22 20:47:08
基于bootstrap jquery.validate form表单验证实践项目结构 : github 上地址:https://github.com/starzou/fron...

基于bootstrap jquery.validate form表单验证实践项目结构 :

基于Bootstrap+jQuery.validate实现Form表单验证

github 上地址:https://github.com/starzou/front-end-example

1、form 表单代码[html]

 

代码如下:


<!doctype html> 
<html> 
    <head> 
        <title>bootstrap form template</title> 
        <meta charset="utf-8" /> 
        <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
        <link rel="stylesheet" type="text/css" href="plugins/bootstrap/css/bootstrap.css"/> 
    </head> 
    <body> 
        <p class="container"> 
            <h1 class="text-center text-danger">form 示例</h1> 
            <form role="form" class="form-horizontal" action="javascript:alert('验证成功,可以提交.');" method="post"> 
                <p class="form-group"> 
                    <label class="col-md-2 control-label" for="name">name</label> 
                    <p class="col-md-10"> 
                        <input class="form-control" name="name" type="text" id="name" placeholder="name" value="" /> 
                    </p> 
                </p> 
                <p class="form-group"> 
                    <label class="col-md-2 control-label" for="exampleinputpassword1">password</label> 
                    <p class="col-md-10"> 
                        <input type="password" name="password" class="form-control" id="exampleinputpassword1" placeholder="password"> 
                    </p> 
                </p> 
                <p class="form-group"> 
                    <label for="intro" class="control-label col-md-2">intro</label> 
                    <p class="col-md-10"> 
                        <textarea id="intro" class="form-control" rows="3" name="intro" placeholder="intro"></textarea> 
                    </p> 
                </p> 
                <p class="form-group"> 
                    <label class="control-label col-md-2">gender</label> 
                    <p class="col-md-10"> 
                        <label class="radio-inline"> 
                            <input type="radio" name="gender"  value="男" /> 
                            boy </label> 
                        <label class="radio-inline"> 
                         &nnbsp;  <input type="radio" name="gender"  value="女" /> 
                            gril </label> 
                    </p> 
                </p> 
                <p class="form-group"> 
                    <label for="hobby" class="control-label col-md-2">hobby</label> 
                    <p class="col-md-10"> 
                        <p class="checkbox"> 
                            <label> 
                                <input type="checkbox" name="hobby" value="music"> 
                                music</label> 
                        </p> 
                        <p class="checkbox"> 
                            <label> 
                                <input type="checkbox" name="hobby" id="" value="game" /> 
                                game </label> 
                        </p> 
                        <label class="checkbox-inline"> 
                            <input type="checkbox" id="inlinecheckbox1" value="option1"> 
                            option1 </label> 
                        <label class="checkbox-inline"> 
                            <input type="checkbox" id="inlinecheckbox2" value="option2"> 
                            option3</label> 
                        <label class="checkbox-inline"> 
                            <input type="checkbox" id="inlinecheckbox3" value="option3"> 
                            option3 </label> 
                    </p> 
                </p> 
                <p class="form-group"> 
                    <label for="sel" class="control-label col-md-2">select</label> 
                    <p class="col-md-10"> 
                        <select multiple="" id="sel" name="sel" class="form-control"> 
                            <option value="1">1</option> 
                            <option value="2">2</option> 
                            <option value="3">3</option> 
                        </select> 
                    </p> 
                </p> 
                <p class="form-group"> 
                    <p class="col-md-offset-2 col-md-10"> 
                        <button type="submit" class="btn btn-primary btn-sm"> 
                            submit 
                        </button> 
                        <button type="reset" class="btn btn-primary btn-sm"> 
                            reset 
                        </button> 
                    </p> 
                </p> 
            </form> 
        </p> 
        <script src="plugins/jquery-1.11.1.js" type="text/javascript" charset="utf-8"></script> 
        <script src="plugins/bootstrap/js/bootstrap.js" type="text/javascript" charset="utf-8"></script> 
        <script src="plugins/jquery-validation/dist/jquery.validate.js" type="text/javascript" charset="utf-8"></script> 
        <script src="scripts/form.js" type="text/javascript" charset="utf-8"></script> 
        <script type="text/javascript" charset="utf-8"> 
            myvalidator.init(); 
        </script> 
    </body> 
</html> 

 

需要引用jquery.js,bootstrap.js,jquery.validate.js 库

2、form.js 代码[javascript]

 

代码如下:


var myvalidator = function() { 
    var handlesubmit = function() { 
        $('.form-horizontal').validate({ 
            errorelement : 'span', 
            errorclass : 'help-block', 
            focusinvalid : false, 
            rules : { 
                name : { 
                    required : true 
                }, 
                password : { 
                    required : true 
                }, 
                intro : { 
                    required : true 
                } 
            }, 
            messages : { 
                name : { 
                    required : "username is required." 
                }, 
                password : { 
                    required : "password is required." 
                }, 
                intro : { 
                    required : "intro is required." 
                } 
            }, 
            highlight : function(element) { 
                $(element).closest('.form-group').addclass('has-error'); 
            }, 
            success : function(label) { 
                label.closest('.form-group').removeclass('has-error'); 
                label.remove(); 
            }, 
            errorplacement : function(error, element) { 
                element.parent('p').append(error); 
            }, 
            submithandler : function(form) { 
                form.submit(); 
            } 
        }); 
        $('.form-horizontal input').keypress(function(e) { 
            if (e.which == 13) { 
                if ($('.form-horizontal').validate().form()) { 
                    $('.form-horizontal').submit(); 
                } 
                return false; 
            } 
        }); 
    } 
    return { 
        init : function() { 
            handlesubmit(); 
        } 
    }; 
}(); 

 

效果 :

基于Bootstrap+jQuery.validate实现Form表单验证

基于Bootstrap+jQuery.validate实现Form表单验证

基于Bootstrap+jQuery.validate实现Form表单验证

基于Bootstrap+jQuery.validate实现Form表单验证

相当不错的一个表单验证的特效,这里推荐给大家,小伙伴们*美化下就可以用到自己项目中了。