本文摘自PHP中文网,作者青灯夜游,侵删。

本篇推荐一款twitter做的BootstrapValidator,本身Bootstrap就是twitter做的,那么使用原配的validator也就更值得信赖。从百度上搜BootstrapValidator会出现很多款,但我只推荐这款(突然感觉自己有点“库【Steve 库里】吹”的感觉)。
相关教程推荐:《bootstrap教程》

###一、一睹为快

为了简便的介绍,这里只做为空的check。
BootstrapValidator官方下载地址
###二、资源引用
下载完资源包后,你可以看到如下的目录。

然后把以下三个文件引入到你项目。
1 2 3 | <link type= "text/css" rel= "stylesheet" href= "${ctx}/components/validate/css/bootstrapValidator.css" />
<script type= "text/javascript" src= "${ctx}/components/validate/js/bootstrapValidator.js" ></script>
<script type= "text/javascript" src= "${ctx}/components/validate/js/language/zh_CN.js" ></script>
|
###三、会员名称不为空项目配置
1 2 3 4 5 6 7 8 9 | <form class= "form-signin required-validate" action= "${ctx}/login" method= "post" οnsubmit= "return validateCallback(this)" >
<div class= "form-group" >
<div class= "row" >
<label>账户</label>
<input class= "form-control" type= "text" autofocus name= "username" placeholder= "请输入会员编号" autocomplete= "off"
data-bv-notempty />
</div>
</div>
</form>
|
data-bv-notempty表示该会员编号要做为空check。
form-group的p是必须的,否则会报“too much recursion”错误。
form表单提交的时候会执行validateCallback方法,该方法在第五步中具体介绍。
###四、页面加载完成后启用bootstrap validator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | $( function () {
$( "form.required-validate" ).each( function () {
var $form = $( this );
$form.bootstrapValidator();
$form.on( 'success.form.bv' , function (e) {
e.preventDefault();
});
});
});
|
###五、form表单提交时验证项目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | function validateCallback(form, callback, confirmMsg) {
YUNM.debug( "进入到form表单验证和提交" );
var $form = $(form);
var data = $form.data( 'bootstrapValidator' );
if (data) {
data.validate();
if (!data.isValid()) {
return false ;
}
}
$.ajax({
type : form.method || 'POST' ,
url : $form.attr( "action" ),
data : $form.serializeArray(),
dataType : "json" ,
cache : false ,
success : callback || YUNM.ajaxDone,
error : YUNM.ajaxError });
return false ;}
|
更多编程相关知识,请访问:编程入门!!
以上就是浅谈bootstrap表单验证插件BootstrapValidator的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
bootstrap4有哪些版本?
bootstrap怎么给input加图标
bootstrap sass 如何使用
bootstrap与vue区别
vue cli3引入bootstrap的方法介绍
bootstrap分页表格插件使用教程
如何运用bootstrap进行页面布局
怎么引入bootstrap
bootstrap组件怎么使用
bootstrap响应式布局怎么实现
更多相关阅读请进入《bootstrap》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » 浅谈bootstrap表单验证插件BootstrapValidator