公司簡介
發(fā)展歷程
高端網(wǎng)站建設(shè)
微信開發(fā)
APP開發(fā)
網(wǎng)絡(luò)營銷服務(wù)
電商網(wǎng)站定制
生物醫(yī)藥網(wǎng)站建設(shè)
外貿(mào)網(wǎng)站建設(shè)
教育培訓(xùn)網(wǎng)站建設(shè)
小程序開發(fā)
功能應(yīng)用
客戶案例
網(wǎng)站建設(shè)案例
小程序案例
電商平臺(tái)案例
APP案例
系統(tǒng)平臺(tái)案例
網(wǎng)站建設(shè)
網(wǎng)站設(shè)計(jì)
常見問題
小程序
公司地址
人才招聘
地址:成都市太升南路288號(hào)錦天國際A幢1002號(hào)
電話:028-86922220
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)
這篇文章給大家分享的是有關(guān)Bootstrap中Validator如何實(shí)現(xiàn)注冊校驗(yàn)和登錄錯(cuò)誤提示效果的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供郴州網(wǎng)站建設(shè)、郴州做網(wǎng)站、郴州網(wǎng)站設(shè)計(jì)、郴州網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、郴州企業(yè)網(wǎng)站模板建站服務(wù),十載郴州做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
1、介紹
在AdminEAP框架中,使用了BootstrapValidator校驗(yàn)框架,本文以注冊校驗(yàn)的用戶名、登錄名、密碼、確認(rèn)密碼的校驗(yàn)(后面還有時(shí)間區(qū)間、服務(wù)器校驗(yàn))為例,講述BootstrapValidator的使用。同時(shí)以登錄錯(cuò)誤提示為例,說明如何在動(dòng)態(tài)改變組件的錯(cuò)誤提示信息。
先看下面的注冊與登錄的校驗(yàn)效果圖:
注冊校驗(yàn):
登錄錯(cuò)誤提示:根據(jù)不同的錯(cuò)誤類型,動(dòng)態(tài)改變組件的樣式和錯(cuò)誤提示內(nèi)容
2、注冊校驗(yàn)
1、頭部引入bootstrap-validator.css
復(fù)制代碼 代碼如下:
${basePath}為系統(tǒng)的路徑變量
2、form組件
3、引入bootstrap-validator.js
4、校驗(yàn)的核心js代碼
3、LoginValidator組件的代碼 login.js
/** * Created by billJiang on 2017/1/12. * 登錄異常信息顯示 */ function LoginValidator(config) { this.code = config.code; this.message = config.message; this.userName = config.userName; this.password = config.password; this.initValidator(); } //0 未授權(quán) 1 賬號(hào)問題 2 密碼錯(cuò)誤 3 賬號(hào)密碼錯(cuò)誤 LoginValidator.prototype.initValidator = function () { if (!this.code) return; if(this.code==0){ this.addPasswordErrorMsg(); }else if(this.code==1){ this.addUserNameErrorStyle(); this.addUserNameErrorMsg(); }else if(this.code==2){ this.addPasswordErrorStyle(); this.addPasswordErrorMsg(); }else if(this.code==3){ this.addUserNameErrorStyle(); this.addPasswordErrorStyle(); this.addPasswordErrorMsg(); } return; } LoginValidator.prototype.addUserNameErrorStyle = function () { this.addErrorStyle(this.userName); } LoginValidator.prototype.addPasswordErrorStyle = function () { this.addErrorStyle(this.password); } LoginValidator.prototype.addUserNameErrorMsg = function () { this.addErrorMsg(this.userName); } LoginValidator.prototype.addPasswordErrorMsg = function () { this.addErrorMsg(this.password); } LoginValidator.prototype.addErrorMsg=function(field){ $("input[name='"+field+"']").parent().append('' + this.message + ''); } LoginValidator.prototype.addErrorStyle=function(field){ $("input[name='" + field + "']").parent().addClass("has-error"); }
以上把錯(cuò)誤提示封裝成了一個(gè)LoginValidator組件,方便前端調(diào)用,增強(qiáng)代碼的可維護(hù)性,因?yàn)闆]有找到Bootstrap-validator改變錯(cuò)誤提示的接口,所以查看了源碼之后做了封裝。
4、補(bǔ)充
1、時(shí)間區(qū)間校驗(yàn)
"startTime":{ validators:{ date:{ format:'YYYY-MM-DD HH:mm', message:'日期格式不正確' }, callback:{ callback:function(value,validator){ var startTime=value; var endTime=$("#endTime").val(); if(startTime&&endTime){ return DateDiff(endTime,startTime)>0; }else{ return true; } }, message:'結(jié)束時(shí)間不能小于開始時(shí)間' } } }, "endTime":{ validators:{ date:{ format:'YYYY-MM-DD HH:mm', message:'日期格式不正確' }, callback:{ callback:function(value,validator){ var startTime=$("#startTime").val(); var endTime=value; if(startTime&&endTime){ return DateDiff(endTime,startTime)>0; }else{ return true; } }, message:'結(jié)束時(shí)間不能小于開始時(shí)間' } } },
2、服務(wù)器校驗(yàn)
"jobClass": { validators: { notEmpty: {message: '執(zhí)行類名不能為空'}, remote:{ url:basePath+"/job/checkJobClass", data: function(validator) { return { jobClass:$('#jobClass').val(), }; }, message:'該執(zhí)行類不存在' } } }
后臺(tái)代碼
@RequestMapping(value="/checkJobClass",method = RequestMethod.POST) @ResponseBody public Map checkJobClass(String jobClass){ Map map=new HashMap<>(); try { Class objClass = Class.forName(jobClass); if(objClass!=null) map.put("valid", true); return map; } catch (Exception ex) { logger.error(ex.getMessage().toString()); map.put("valid", false); return map; } }
感謝各位的閱讀!關(guān)于“Bootstrap中Validator如何實(shí)現(xiàn)注冊校驗(yàn)和登錄錯(cuò)誤提示效果”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!