金狮镖局 Design By www.egabc.com

基于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> 
        <div class="container"> 
            <h1 class="text-center text-danger">Form 示例</h1> 
            <form role="form" class="form-horizontal" action="javascript:alert('验证成功,可以提交.');" method="post"> 
                <div class="form-group"> 
                    <label class="col-md-2 control-label" for="name">Name</label> 
                    <div class="col-md-10"> 
                        <input class="form-control" name="name" type="text" id="name" placeholder="Name" value="" /> 
                    </div> 
                </div> 
                <div class="form-group"> 
                    <label class="col-md-2 control-label" for="exampleInputPassword1">Password</label> 
                    <div class="col-md-10"> 
                        <input type="password" name="password" class="form-control" id="exampleInputPassword1" placeholder="Password"> 
                    </div> 
                </div> 
                <div class="form-group"> 
                    <label for="intro" class="control-label col-md-2">Intro</label> 
                    <div class="col-md-10"> 
                        <textarea id="intro" class="form-control" rows="3" name="intro" placeholder="Intro"></textarea> 
                    </div> 
                </div> 
                <div class="form-group"> 
                    <label class="control-label col-md-2">Gender</label> 
                    <div class="col-md-10"> 
                        <label class="radio-inline"> 
                            <input type="radio" name="gender"  value="男" /> 
                            boy </label> 
                        <label class="radio-inline"> 
                            <input type="radio" name="gender"  value="女" /> 
                            gril </label> 
                    </div> 
                </div> 
                <div class="form-group"> 
                    <label for="hobby" class="control-label col-md-2">Hobby</label> 
                    <div class="col-md-10"> 
                        <div class="checkbox"> 
                            <label> 
                                <input type="checkbox" name="hobby" value="Music"> 
                                Music</label> 
                        </div> 
                        <div class="checkbox"> 
                            <label> 
                                <input type="checkbox" name="hobby" id="" value="Game" /> 
                                Game </label> 
                        </div> 
                        <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> 
                    </div> 
                </div> 
                <div class="form-group"> 
                    <label for="sel" class="control-label col-md-2">Select</label> 
                    <div 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> 
                    </div> 
                </div> 
                <div class="form-group"> 
                    <div 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> 
                    </div> 
                </div> 
            </form> 
        </div> 
        <script src="/UploadFiles/2021-04-02/jquery-1.11.1.js">         <script src="plugins/bootstrap/js/bootstrap.js">         <script src="/UploadFiles/2021-04-02/jquery.validate.js">         <script src="scripts/form.js">         <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('div').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表单验证

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

标签:
Bootstrap,jQuery.validate,表单验证

金狮镖局 Design By www.egabc.com
金狮镖局 免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金狮镖局 Design By www.egabc.com

评论“基于Bootstrap+jQuery.validate实现Form表单验证”

暂无基于Bootstrap+jQuery.validate实现Form表单验证的评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。