Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 16:26:25
Original
1122 people have browsed it

基于Bootstrap jQuery.validate Form表单验证实践项目结构 :

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

1、form 表单代码[html]

复制代码 代码如下:

 
 
     
        Bootstrap Form Template 
         
         
         
     
     
       
 
           

Form 示例

 
           
 
               
 
                     
                   
 
                         
                   
 
               
 
               
 
                     
                   
 
                         
                   
 
               
 
               
 
                     
                   
 
                         
                   
 
               
 
               
 
                     
                   
 
                         
                         
                   
 
               
 
               
 
                     
                   
 
                       
 
                             
                       
 
                       
 
                             
                       
 
                         
                         
                         
                   
 
               
 
               
 
                     
                   
 
                         
                   
 
               
 
               
 
                   
 
                         
                         
                   
 
               
 
           
 
       
 
         
         
         
         
         
     
 

需要引用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(); 
        } 
    }; 
}(); 

效果 :

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!