bootstrap:能夠增加兼容性的強(qiáng)大框架.
因?yàn)轫?xiàng)目需要數(shù)據(jù)驗(yàn)證,看bootstrapValidator 還不錯(cuò),就上手一直,完美兼容,話(huà)不多說(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ì)VeVb武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答