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

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

程序员文章站 2022-05-03 20:30:50
...
基于Bootstrap jQuery.validate Form表单验证实践项目结构 :

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

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

效果 :

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

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

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

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

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