Home >Web Front-end >JS Tutorial >Implementing Form form validation based on Bootstrap jQuery.validate_jquery

Implementing Form form validation based on Bootstrap jQuery.validate_jquery

WBOY
WBOYOriginal
2016-05-16 16:26:251197browse

Based on Bootstrap jQuery.validate Form form validation practice project structure:

Source code address on github: https://github.com/starzou/front-end-example

1. form code [html]

Copy code The code is as follows:

 
 
     
        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();                                                                                                                                            };
}();



Effect:

A pretty good special effect for form validation. I recommend it to everyone. Friends can use it in their own projects by beautifying it freely.

Statement:
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