BootstrapValidator驗證用戶名已存在(ajax)
Java web項目:bootstrap實現(xiàn)注冊頁面,mvc模式聯(lián)合mysql數(shù)據(jù)庫檢查用戶名的唯一性。
一、實現(xiàn)效果:

重置這里有bug,bootstrapValidator驗證不能重置,待解決。
二、代碼準備:
引入bootstrap,bootstrapValidator和jquery。
<link rel="stylesheet" href="<%=request.getContextPath() %>/css/bootstrap.min.css" rel="external nofollow" /> <link rel="stylesheet" href="<%=request.getContextPath() %>/css/bootstrapValidator.min.css" rel="external nofollow" />
<script src="<%=request.getContextPath() %>/js/jquery.min.js"></script> <script src="<%=request.getContextPath() %>/js/bootstrap.min.js"></script> <script src="<%=request.getContextPath() %>/js/bootstrapValidator.min.js"></script>
三、部分代碼:
register.jsp注冊部分代碼。
<form id="registerForm" action="<%=request.getContextPath() %>/UserServlet" method="post"> <input type="hidden" name="method" value="register"/> <div class="form-group"> <label>用戶名</label> <input type="text" class="form-control" name="userName" placeholder="用戶名由2-12位字符組成" /> </div> <div class="form-group"> <label>郵箱</label> <input type="text" class="form-control" name="userEmail" placeholder="郵箱" /> </div> <div class="form-group"> <label>密碼</label> <input type="password" class="form-control" name="userPassword" placeholder="密碼由6-10位字母數(shù)字組成" /> </div> <div class="form-group"> <label>確認密碼</label> <input type="password" class="form-control" name="confirmUserPassword" placeholder="再次輸入密碼" /> </div> <div class="form-group"> <button type="submit" class="btn btn-primary">注冊</button> <input type="reset" class="btn btn-primary" value="重置"> </div> </form>
利用bootstrapValidator表單驗證代碼。 ajax部分有詳細注釋
<script type="text/javascript">
$(function() {
$('#registerForm').bootstrapValidator({
message: 'This value is not valid',
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
userName: {
message: 'The username is not valid',
validators: {
notEmpty: {
message: '用戶名不能為空'
},
stringLength: {
min: 2,
max: 12,
message: '用戶名由2-12位字符組成'
},
threshold: 2,//有2字符以上才發(fā)送ajax請求
remote: {//ajax驗證。server result:{"valid",true or false}
url: "/ImageShare/UserServlet",
message: '用戶名已存在,請重新輸入',
delay: 1000,//ajax刷新的時間是1秒一次
type: 'POST',
//自定義提交數(shù)據(jù),默認值提交當前input value
data: function(validator) {
return {
userName : $("input[name=userName]").val(),
method : "checkUserName"http://UserServlet判斷調(diào)用方法關(guān)鍵字。
};
}
}
}
},
userEmail: {
validators: {
notEmpty: {
message: '郵箱不能為空'
},
emailAddress: {
message: '輸入不是有效的電子郵件地址'
}
}
},
userPassword: {
validators: {
notEmpty: {
message: '密碼不能為空'
},
stringLength: {
min: 6,
max: 10,
message: '密碼由6-10位字符組成'
},
identical: {
field: 'confirmUserPassword',
message: '密碼輸入不一致'
}
}
},
confirmUserPassword: {
validators: {
notEmpty: {
message: '密碼不能為空'
},
stringLength: {
min: 6,
max: 10,
message: '密碼由6-10位字符組成'
},
identical: {
field: 'userPassword',
message: '密碼輸入不一致'
}
}
}
}
});
});
</script>
UserServlet.java檢查用戶名唯一性部分代碼。
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
//0、獲取method判斷執(zhí)行操作
String method = request.getParameter("method");
if ("checkUserName".equals(method)) {
//驗證用戶名是否已存在
checkUserName(request,response);
}
}
//根據(jù)用戶名稱查詢,檢查用戶名稱的唯一性(用戶注冊)
public void checkUserName(HttpServletRequest request, HttpServletResponse response) throws IOException{
response.setCharacterEncoding("UTF-8");
//返回json數(shù)據(jù),格式為{"valid",true} 表示合法,驗證通過。{"valid":false} 表示不合法,驗證不通過
String jsonResult = "";
String userName = request.getParameter("userName");
//去數(shù)據(jù)進行唯一性確認
if (userName!=null) {
//服務(wù)層service調(diào)用數(shù)據(jù)庫訪問層dao中的searchUserName方法。
boolean b = UserServiceImpl.searchUserName(userName);
if (b) {
//如果名稱存在
jsonResult = "{\"valid\":false}";
}else{
//如果該名稱不存在
jsonResult = "{\"valid\":true}";
}
} else {
jsonResult = "{\"valid\":false}";
}
//response把jsonResult打到前臺
response.getWriter().write(jsonResult);
}
四、總結(jié):
1.利用bootstrapValidator的ajax表單驗證用戶名已存在關(guān)鍵是自定義提交的數(shù)據(jù)。
2.將當前input的value值和判斷操作方法的method關(guān)鍵字提交
3.注意當server必需返回形如:{“valid”,true or false} 的json數(shù)據(jù)格式
4.servlet通過 response.getWriter().write(jsonResult) 返回響應(yīng)的內(nèi)容jsonResult到前臺頁面。
如果大家還想深入學(xué)習(xí),可以點擊這里進行學(xué)習(xí),再為大家附3個精彩的專題:
以上就是關(guān)于本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
- Bootstrap中的表單驗證插件bootstrapValidator使用方法整理(推薦)
- JS組件Form表單驗證神器BootstrapValidator
- 實用又漂亮的BootstrapValidator表單驗證插件
- jquery插件bootstrapValidator數(shù)據(jù)驗證詳解
- bootstrapValidator bootstrap-select驗證不可用的解決辦法
- 基于jQuery 實現(xiàn)bootstrapValidator下的全局驗證
- jquery插件bootstrapValidator表單驗證詳解
- bootstrapValidator自定驗證方法寫法
- 基于BootstrapValidator的Form表單驗證(24)
- 使用BootStrapValidator完成前端輸入驗證
相關(guān)文章
Javascript 實現(xiàn)的數(shù)獨解題算法網(wǎng)頁實例
此算法的實現(xiàn),就是模擬人腦的思考和計算過程,有需要的朋友可以參考一下2013-10-10
JavaScript函數(shù)柯里化實現(xiàn)原理及過程
這篇文章主要介紹了JavaScript函數(shù)柯里化實現(xiàn)原理及過程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-12-12
webpack構(gòu)建打包的性能優(yōu)化實戰(zhàn)指南
webpack是前端開發(fā)中比較常用的打包工具之一,另外還有g(shù)ulp,grunt,下面這篇文章主要給大家介紹了關(guān)于webpack構(gòu)建打包的性能優(yōu)化的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-03-03
JavaScript實現(xiàn)同一頁面內(nèi)兩個表單互相傳值的方法
這篇文章主要介紹了JavaScript實現(xiàn)同一頁面內(nèi)兩個表單互相傳值的方法,涉及javascript鼠標事件及頁面元素賦值操作的相關(guān)技巧,需要的朋友可以參考下2015-08-08
微信小程序中多個頁面?zhèn)鲄⑼ㄐ诺膶W(xué)習(xí)與實踐
剛接觸微信小程序,對里面的語法和屬性還不怎么了解,最近正在努力學(xué)習(xí)中,下面這篇文章主要給大家介紹了微信小程序中多個頁面?zhèn)鲄⑼ㄐ诺南嚓P(guān)資料,是最近學(xué)習(xí)的一個內(nèi)容總結(jié),需要的朋友可以參考借鑒,下面來一起看看吧。2017-05-05
javascript 極速 隱藏/顯示萬行表格列只需 60毫秒
隱藏表格列 這種方式的效率極低。例如,隱藏一個千行表格的某列,在我的筆記本(P4 M 1.4G,768M內(nèi)存)上執(zhí)行需要約 4000毫秒的時間,令人無法忍受。2009-03-03

