BootstrapValidator實(shí)現(xiàn)表單驗(yàn)證功能
BootstrapValidator是一款非常好用的表單驗(yàn)證插件,通過(guò)友好的錯(cuò)誤提示能增加用戶體驗(yàn)。
bootstrapvalidator源碼下載
既然是表單驗(yàn)證,那我們最常用的就是用戶登錄界面,下面來(lái)看看測(cè)試代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Twitter -->
<meta name="twitter:site" content="@themepixels">
<meta name="twitter:creator" content="@themepixels">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Starlight">
<meta name="twitter:description" content="Premium Quality and Responsive UI for Dashboard.">
<meta name="twitter:image" content="http://themepixels.me/starlight/img/starlight-social.png">
<!-- Facebook -->
<meta property="og:url" content="http://themepixels.me/starlight">
<meta property="og:title" content="Starlight">
<meta property="og:description" content="Premium Quality and Responsive UI for Dashboard.">
<meta property="og:image" content="http://themepixels.me/starlight/img/starlight-social.png">
<meta property="og:image:secure_url" content="http://themepixels.me/starlight/img/starlight-social.png">
<meta property="og:image:type" content="image/png">
<meta property="og:image:width" content="1200">
<meta property="og:image:height" content="600">
<!-- Meta -->
<meta name="description" content="Premium Quality and Responsive UI for Dashboard.">
<meta name="author" content="ThemePixels">
<title>歡迎使用</title>
<!-- vendor css -->
<link href="../lib/font-awesome/css/font-awesome.css" rel="stylesheet">
<link href="../lib/Ionicons/css/ionicons.css" rel="stylesheet">
<!-- Starlight CSS -->
<link rel="stylesheet" href="../css/starlight.css" >
<!-- 將下載好的BootstrapValidator js,css文件導(dǎo)入,配合bootstrap插件使用 -->
<link rel="stylesheet" href="../vendor/bootstrap/css/bootstrap.css" />
<link rel="stylesheet" href="../dist/css/bootstrapValidator.css" />
<script type="text/javascript" src="../vendor/jquery/jquery.min.js"></script>
<script type="text/javascript" src="../vendor/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../dist/js/bootstrapValidator.js"></script>
</head>
<body>
<div class="d-flex align-items-center justify-content-center bg-sl-primary ht-100v">
<div class="login-wrapper wd-300 wd-xs-350 pd-25 pd-xs-40 bg-white">
<div class="signin-logo tx-center tx-24 tx-bold tx-inverse"><span class="tx-info tx-normal">歡迎使用</span>
</div>
<form id="form">
<div class="form-group">
<input type="text" class="form-control" placeholder="請(qǐng)輸入賬號(hào)" name="username">
</div><!-- form-group -->
<div class="form-group">
<input type="password" class="form-control" placeholder="請(qǐng)輸入密碼" name="password">
<a href="" class=" rel="external nofollow" tx-info tx-12 d-block mg-t-10">忘記密碼?</a>
</div><!-- form-group -->
<div class="form-group">
<input type="text" id="checkCodeYZM" name="checkCodeYZM" placeholder="驗(yàn)證碼" class="form-control">
<div class="code" class="form-control">
<canvas id="canvas" width="100" height="36"></canvas>
</div>
</div>
<button type="submit" class="btn btn-info btn-block">登錄</button>
<div class="mg-t-60 tx-center">還沒(méi)有注冊(cè)賬號(hào)? <a href="page-signup.html" class="tx-info">注冊(cè)</a></div>
</form>
</div><!-- login-wrapper -->
</div><!-- d-flex -->
<script src="../lib/popper.js/popper.js"></script>
<script src="../lib/bootstrap/bootstrap.js"></script>
<script>
var show_num = [];//將要生成的隨機(jī)驗(yàn)證碼
draw(show_num);
/**點(diǎn)擊驗(yàn)證碼重新生成*/
$("#canvas").on('click', function () {
draw(show_num);
});
function randomColor() {//得到隨機(jī)的顏色值==>把顏色設(shè)置為黑色,看的清楚
var r = Math.floor(0);
var g = Math.floor(0);
var b = Math.floor(0);
return "rgb(" + r + "," + g + "," + b + ")";
}
function draw(show_num) {
var canvas_width = $('#canvas').width();
var canvas_height = $('#canvas').height();
var canvas = document.getElementById("canvas");//獲取到canvas的對(duì)象,演員
var context = canvas.getContext("2d");//獲取到canvas畫(huà)圖的環(huán)境,演員表演的舞臺(tái)
canvas.width = canvas_width;
canvas.height = canvas_height;
var sCode = "1,2,3,4,5,6,7,8,9,0";
var aCode = sCode.split(",");
var aLength = aCode.length;//獲取到數(shù)組的長(zhǎng)度
for (var i = 0; i <= 3; i++) {
var j = Math.floor(Math.random() * aLength);//獲取到隨機(jī)的索引值
var deg = Math.random() * 30 * Math.PI / 180;//產(chǎn)生0~30之間的隨機(jī)弧度
var txt = aCode[j];//得到隨機(jī)的一個(gè)內(nèi)容
show_num[i] = txt.toLowerCase();
var x = 10 + i * 20;//文字在canvas上的x坐標(biāo)
var y = 20 + Math.random() * 8;//文字在canvas上的y坐標(biāo)
context.font = "bold 26px 楷體";
context.translate(x, y);
context.rotate(deg);
context.fillStyle = randomColor();
context.fillText(txt, 0, 0);
context.rotate(-deg);
context.translate(-x, -y);
}
for (var i = 0; i <= 5; i++) { //驗(yàn)證碼上顯示線條
context.strokeStyle = randomColor();
context.beginPath();
context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);
context.stroke();
}
for (var i = 0; i <= 30; i++) { //驗(yàn)證碼上顯示小點(diǎn)
context.strokeStyle = randomColor();
context.beginPath();
var x = Math.random() * canvas_width;
var y = Math.random() * canvas_height;
context.moveTo(x, y);
context.lineTo(x + 1, y + 1);
context.stroke();
}
//為了測(cè)試方便不用每次都輸入驗(yàn)證碼
//$("#checkCodeYZM").val(show_num.join(""));
}
$(document).ready(function () {
//當(dāng)提交時(shí)進(jìn)行表單驗(yàn)證 代碼內(nèi)容容易看懂 這里不進(jìn)行注解解釋
$('#form').bootstrapValidator({
message: 'This value is not valid',
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
username: {
message: '用戶名驗(yàn)證失敗',
validators: {
notEmpty: {
message: '用戶名不能為空'
},
stringLength: {
min: 6,
max: 30,
message: '用戶名必須大于6位且不得超過(guò)30位字符'
},
}
},
password: {
message: '密碼驗(yàn)證失敗',
validators: {
notEmpty: {
message: '密碼不能為空'
},stringLength: {
min: 8,
max: 20,
message: '密碼必須大于8位且不得超過(guò)20位字符'
},
}
},
checkCodeYZM: {
message: '驗(yàn)證碼驗(yàn)證失敗',
validators: {
notEmpty: {
message: '驗(yàn)證碼不能為空'
}
}
}
}
});
});
</script>
</body>
</html>
下面看看測(cè)試效果:
當(dāng)不輸入內(nèi)容直接提交時(shí),提示不能為空:

當(dāng)輸入字符不符合規(guī)定位數(shù)時(shí),提示:

可根據(jù)需求繼續(xù)添加表單驗(yàn)證
如果大家還想深入學(xué)習(xí),可以點(diǎn)擊這里進(jìn)行學(xué)習(xí),再為大家附3個(gè)精彩的專題:
以上就是關(guān)于本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- vue props對(duì)象validator自定義函數(shù)實(shí)例
- Spring Validator接口校驗(yàn)與全局異常處理器
- laravel 解決Validator使用中出現(xiàn)的問(wèn)題
- SpringBoot 使用hibernate validator校驗(yàn)
- 如何優(yōu)雅的使用 laravel 的 validator驗(yàn)證方法
- Spring中校驗(yàn)器(Validator)的深入講解
- Spring MVC+FastJson+hibernate-validator整合的完整實(shí)例教程
- springboot使用Validator校驗(yàn)方式
- springboot使用hibernate validator校驗(yàn)方式
- springboot validator枚舉值校驗(yàn)功能實(shí)現(xiàn)
相關(guān)文章
JS 的應(yīng)用開(kāi)發(fā)初探(mootools)
昨天在公司內(nèi)部做了一個(gè)小小的技術(shù)分享,就 js 應(yīng)用開(kāi)發(fā)方面跟大家談了一點(diǎn)自己的心得,最近因?yàn)楣ぷ麝P(guān)系花在這上面的時(shí)間較多也頗有些收獲,寫(xiě)在這里備忘。2009-12-12
JavaScript實(shí)現(xiàn)簡(jiǎn)單獲取本地圖片主色調(diào)
想象一個(gè)場(chǎng)景,就是如何根據(jù)一張圖片大概提取出它的主色調(diào)呢?獲取主色調(diào)后,可能會(huì)用來(lái)設(shè)置某些背景顏色,這里,利用?JS?簡(jiǎn)單獲取本地圖片主色調(diào),希望對(duì)大家有所幫助2023-03-03
通過(guò)Tabs方法基于easyUI+bootstrap制作工作站
本教程給大家介紹如何制作easyUI+bootstrap工作站,主要學(xué)習(xí)tabs方法,本文介紹非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友參考下吧2016-03-03
js獲取頁(yè)面?zhèn)鱽?lái)參數(shù)的方法
這篇文章主要介紹了通過(guò)window.location.search來(lái)獲取頁(yè)面?zhèn)鱽?lái)的參數(shù),經(jīng)測(cè)試是OK的2014-09-09
原生JavaScript實(shí)現(xiàn)輪播圖效果
這篇文章主要為大家詳細(xì)介紹了原生JavaScript實(shí)現(xiàn)輪播圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
超贊的動(dòng)手創(chuàng)建JavaScript框架的詳細(xì)教程
這篇文章主要介紹了動(dòng)手創(chuàng)建JavaScript框架的詳細(xì)教程,包括DOM和各種屬性的調(diào)試等各個(gè)方面,超級(jí)推薦!需要的朋友可以參考下2015-06-06
JavaScript+html實(shí)現(xiàn)前端頁(yè)面滑動(dòng)驗(yàn)證(2)
這篇文章主要為大家詳細(xì)介紹了JavaScript+html實(shí)現(xiàn)前端頁(yè)面滑動(dòng)驗(yàn)證的第二種方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
javascript和jQuery實(shí)現(xiàn)網(wǎng)頁(yè)實(shí)時(shí)聊天的ajax長(zhǎng)輪詢
在做網(wǎng)頁(yè)實(shí)時(shí)聊天的時(shí)候常常需要長(zhǎng)輪詢,本文由于采用原生的JS及AJAX,所以簡(jiǎn)單易懂,通過(guò)這篇文章就可以建立一個(gè)簡(jiǎn)單的聊天室程序。2016-07-07

