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

 更新时间:2014年12月16日 09:45:08   投稿:hebedich  
这篇文章主要介绍了基于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="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('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(); 
        } 
    }; 
}(); 

效果 :

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

相关文章

  • jquery 插件学习(六)

    jquery 插件学习(六)

    把其中的参数默认值作为$.fn.color对象的属性单独进行设计,然后借助jquery.extend方法,覆盖原来的参数选项即可
    2012-08-08
  • ThinkPHP+jquery实现“加载更多”功能代码

    ThinkPHP+jquery实现“加载更多”功能代码

    本篇文章主要介绍了ThinkPHP+jquery实现“加载更多”功能代码,以实例代码讲诉了加载更多的代码实现,非常具有实用价值,需要的朋友可以参考下
    2017-03-03
  • jquery实现简单的无缝滚动

    jquery实现简单的无缝滚动

    这里给大家分享的是使用jQuery实现简单的无缝滚动的效果,其思路是我们通过js控制 ul 标签的margin 来实现滚动。横向滚动则是控制 margin-left ; 纵向滚动则是控制 margin-top;,有需要的小伙伴可以参考下。
    2015-04-04
  • jQuery常用知识点总结以及平时封装常用函数

    jQuery常用知识点总结以及平时封装常用函数

    这篇文章主要介绍了jQuery常用知识点总结以及平时封装常用函数 的相关资料,需要的朋友可以参考下
    2016-02-02
  • jQuery plugin animsition使用小结

    jQuery plugin animsition使用小结

    本文通过实例代码给大家分享了jQuery plugin animsition用法,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2017-09-09
  • jQuery实现锁定页面元素(表格列)

    jQuery实现锁定页面元素(表格列)

    这篇文章主要为大家详细介绍了jQuery实现锁定页面元素,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • jQuery之关于resize()方法的使用

    jQuery之关于resize()方法的使用

    这篇文章主要介绍了jQuery之关于resize()方法的使用,具有很好的价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • 深入理解jQuery3.0的domManip函数

    深入理解jQuery3.0的domManip函数

    domManip函数可以说是jquery中一个元老级工具函数了,domManip 的主要功能是为了实现 DOM 的插入和替换。接下来通过本文给大家谈谈jQuery3.0的domManip函数的理解,非常不错,感兴趣的朋友一起看看吧
    2016-09-09
  • jQuery插件实现表格隔行变色及鼠标滑过高亮显示效果代码

    jQuery插件实现表格隔行变色及鼠标滑过高亮显示效果代码

    这篇文章主要介绍了jQuery插件实现表格隔行变色及鼠标滑过高亮显示效果代码,涉及jQuery针对页面元素动态操作及响应鼠标事件动态修改页面元素样式的相关技巧,需要的朋友可以参考下
    2016-02-02
  • jQuery跨域问题解决方案

    jQuery跨域问题解决方案

    通过XMLHTTPRquest请求不同域上的数据,原来js跨域访问是后台有个处理路径“/test”的函数。下面给大家介绍jQuery跨域问题解决方案,有需要的小伙伴可以参考下
    2015-08-08

最新评论