BootStrapValidator初使用教程詳解
bootstrap:能夠增加兼容性的強(qiáng)大框架.
因?yàn)轫?xiàng)目需要數(shù)據(jù)驗(yàn)證,看bootstrapValidator 還不錯(cuò),就上手一直,完美兼容,話不多說(shuō)。
需要引用css:
bootstrap.min.css
bootstrapValidator.min.css
js:
jQuery-1.10.2.min.js
bootstrap.min.js
bootstrapValidator.min.js
以上這些都是必須的。
先上個(gè)簡(jiǎn)單的例子,只要導(dǎo)入相應(yīng)的文件可以直接運(yùn)行:
<!DOCTYPE html>
<html>
<head>
<title>Using Ajax to submit data</title>
<link rel="stylesheet" href="css/bootstrap.css" rel="external nofollow" />
<link rel="stylesheet" href="css/bootstrapValidator.css" rel="external nofollow" />
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/bootstrapValidator.js"></script>
</head>
<body>
<div class="container">
<!-- class都是bootstrap定義好的樣式,驗(yàn)證是根據(jù)input中的name值 -->
<form id="defaultForm" method="post" class="form-horizontal" action="aaa.html">
<!-- 下面這個(gè)div必須要有,插件根據(jù)這個(gè)進(jìn)行添加提示 -->
<div class="form-group">
<label class="col-lg-3 control-label">Username</label>
<div class="col-lg-5">
<input type="text" class="form-control" name="username" />
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Email address</label>
<div class="col-lg-5">
<input type="text" class="form-control" name="email" />
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Password</label>
<div class="col-lg-5">
<input type="password" class="form-control" name="password" />
</div>
</div>
<div class="form-group">
<div class="col-lg-9 col-lg-offset-3">
<button type="submit" class="btn btn-primary">Sign up</button>
</div>
</div>
</form>
</div>
<script type="text/javascript">
$(document).ready(function() {
/**
* 下面是進(jìn)行插件初始化
* 你只需傳入相應(yīng)的鍵值對(duì)
* */
$('#defaultForm').bootstrapValidator({
message: 'This value is not valid',
feedbackIcons: {/*輸入框不同狀態(tài),顯示圖片的樣式*/
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {/*驗(yàn)證*/
username: {/*鍵名username和input name值對(duì)應(yīng)*/
message: 'The username is not valid',
validators: {
notEmpty: {/*非空提示*/
message: '用戶(hù)名不能為空'
},
stringLength: {/*長(zhǎng)度提示*/
min: 6,
max: 30,
message: '用戶(hù)名長(zhǎng)度必須在6到30之間'
}/*最后一個(gè)沒(méi)有逗號(hào)*/
}
},
password: {
message:'密碼無(wú)效',
validators: {
notEmpty: {
message: '密碼不能為空'
},
stringLength: {
min: 6,
max: 30,
message: '用戶(hù)名長(zhǎng)度必須在6到30之間'
}
}
},
email: {
validators: {
notEmpty: {
message: 'The email address is required and can\'t be empty'
},
emailAddress: {
message: 'The input is not a valid email address'
}
}
}
}
});
});
</script>
</body>
</html>
當(dāng)然,以上都是插件寫(xiě)好的規(guī)則,如果想自己加匹配規(guī)則怎么辦呢?
如下只要在input相對(duì)應(yīng)的鍵值中加入一個(gè)regexp:{}鍵值對(duì)(在上面的js基礎(chǔ)上修改)
username: {/*鍵名和input name值對(duì)應(yīng)*/
message: 'The username is not valid',
validators: {
notEmpty: {/*非空提示*/
message: '用戶(hù)名不能為空'
},
regexp: {/* 只需加此鍵值對(duì),包含正則表達(dá)式,和提示 */
regexp: /^[a-zA-Z0-9_\.]+$/,
message: '只能是數(shù)字和字母_.'
},
stringLength: {/*長(zhǎng)度提示*/
min: 6,
max: 30,
message: '用戶(hù)名長(zhǎng)度必須在6到30之間'
}/*最后一個(gè)沒(méi)有逗號(hào)*/
}
},
以上所述是小編給大家介紹的BootStrapValidator初使用教程詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- bootstrap datepicker 與bootstrapValidator同時(shí)使用時(shí)選擇日期后無(wú)法正常觸發(fā)校驗(yàn)的解決思路
- 使用bootstrapValidator插件進(jìn)行動(dòng)態(tài)添加表單元素并校驗(yàn)
- BootStrap Validator使用注意事項(xiàng)(必看篇)
- 使用BootStrapValidator完成前端輸入驗(yàn)證
- 使用bootstrap validator的remote驗(yàn)證代碼經(jīng)驗(yàn)分享(推薦)
- BootStrap與validator 使用筆記(JAVA SpringMVC實(shí)現(xiàn))
- Bootstrap中的表單驗(yàn)證插件bootstrapValidator使用方法整理(推薦)
相關(guān)文章
微信小程序?qū)崿F(xiàn)動(dòng)態(tài)驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)動(dòng)態(tài)驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
JavaScript中undefined、null與NaN的區(qū)別
undefined、null和NaN都屬于javascript中的數(shù)據(jù)類(lèi)型,本文主要介紹了 JavaScript中undefined、null與NaN的區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
JavaScript降低代碼圈復(fù)雜度優(yōu)化技巧
當(dāng)一個(gè)項(xiàng)目經(jīng)過(guò)持續(xù)迭代,不斷增加功能,逐漸變成一個(gè)復(fù)雜的產(chǎn)品時(shí),新功能的開(kāi)發(fā)變得相對(duì)困難,其中一個(gè)很大的原因是代碼復(fù)雜度高,導(dǎo)致可維護(hù)性和可讀性都很差,本文將從前端JavaScript的角度出發(fā),介紹一些有效的方法和技巧來(lái)優(yōu)化前端代碼的圈復(fù)雜度2023-10-10
使用百度地圖實(shí)現(xiàn)地圖網(wǎng)格的示例
下面小編就為大家分享一篇使用百度地圖實(shí)現(xiàn)地圖網(wǎng)格的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
純js實(shí)現(xiàn)的積木(div層)拖動(dòng)功能示例
這篇文章主要介紹了純js實(shí)現(xiàn)的積木(div層)拖動(dòng)功能,結(jié)合實(shí)例形式分析了javascript隨機(jī)生成各種顏色div層及響應(yīng)鼠標(biāo)事件改變?cè)貙傩詫?shí)現(xiàn)拖動(dòng)效果的相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
不用ajax實(shí)現(xiàn)點(diǎn)擊文字即可編輯的方法
本文給大家分享一段代碼不使用ajax實(shí)現(xiàn)點(diǎn)擊文字即可編輯的方法,代碼簡(jiǎn)單易懂,需要的朋友參考下吧2007-12-12

