BootstrapValidator驗證用戶名已存在(ajax)
Java web項目:bootstrap實現(xiàn)注冊頁面,mvc模式聯(lián)合mysql數(shù)據(jù)庫檢查用戶名的唯一性。
一、實現(xiàn)效果:
重置這里有bug,bootstrapValidator驗證不能重置,待解決。
二、代碼準(zhǔn)備:
引入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>確認(rèn)密碼</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部分有詳細(xì)注釋
<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ù),默認(rèn)值提交當(dāng)前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ù)進(jìn)行唯一性確認(rèn) 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.將當(dāng)前input的value值和判斷操作方法的method關(guān)鍵字提交
3.注意當(dāng)server必需返回形如:{“valid”,true or false} 的json數(shù)據(jù)格式
4.servlet通過 response.getWriter().write(jsonResult) 返回響應(yīng)的內(nèi)容jsonResult到前臺頁面。
如果大家還想深入學(xué)習(xí),可以點擊這里進(jìn)行學(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獲取與設(shè)置ckeditor數(shù)據(jù)的實現(xiàn)方法
最近編輯器后臺升級成了ckeditor,但原來后臺有很多對應(yīng)編輯器內(nèi)容的替換功能,那么就需要用js獲取ckeditor編輯器里面的內(nèi)容,這里就為大家介紹一下具體的實現(xiàn)方法2023-08-08瀑布流的實現(xiàn)方式(原生js+jquery+css3)
這篇文章主要為大家詳細(xì)介紹了原生js+jquery+css3實現(xiàn)瀑布流的相關(guān)代碼,三種實現(xiàn)瀑布流的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-07-07javascript中setAttribute()函數(shù)使用方法及兼容性
這篇文章主要介紹了javascript中setAttribute()函數(shù)使用方法及兼容性的相關(guān)資料,需要的朋友可以參考下2015-07-07JS控件bootstrap suggest plugin使用方法詳解
這篇文章主要介紹了JS控件bootstrap suggest plugin的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03JavaScript實現(xiàn)左右下拉框動態(tài)增刪示例
本篇文章主要介紹了JavaScript實現(xiàn)左右下拉框動態(tài)增刪示例,可以對下拉框進(jìn)行刪除和增加,非常具有實用價值,需要的朋友可以參考下。2017-03-03