Angualrjs 表單驗(yàn)證的兩種方式(失去焦點(diǎn)驗(yàn)證和點(diǎn)擊提交驗(yàn)證)
AngularJS提供了表單驗(yàn)證,但是驗(yàn)證的過程交互體驗(yàn)很不好,比如重設(shè)密碼,重復(fù)密碼的時(shí)候一鍵入就會(huì)提示密碼不正確,現(xiàn)整理了兩種方法,僅供借鑒。
一,點(diǎn)擊提交驗(yàn)證
<form action="" class="form-horizontal col-md-9" name="reset_pwd" ng-submit="resetPwd()"> <div class="form-group"> <label class="col-sm-2 control-label">密碼</label> <div class="col-sm-8"> <input type="password" name="mycompwd" class="form-control" ng-model="mycompwd" placeholder="請(qǐng)輸入密碼"> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">重復(fù)密碼</label> <div class="col-sm-8"> <input type="password" name="resetmycompwd" class="form-control" ng-model="resetmycompwd" placeholder="再次輸入密碼" required> </div> <span style="color:red" ng-show="mycompwd!=resetmycompwd && reset_pwd.resetmycompwd.$dirty && reset_pwd.submitted ">密碼不一致</span> </div> <div class="form-group"> <button type="submit" class="btn btn-primary" >確認(rèn)</button> <button type="button" class="btn btn-default" ng-click="resetpwd_cancle()">取消</button> </div> </form>
當(dāng)用戶試圖提交表單時(shí),你可以在作用域中捕獲到一個(gè)submitted值,然后對(duì)表單內(nèi)容進(jìn)行驗(yàn)證并顯示錯(cuò)誤信息。
JS代碼
$scope.submitted = false; $scope.resetPwd = function(){ console.log(666); if($scope.reset_pwd.$valid && $scope.mycompwd == $scope.resetmycompwd){ console.log('重置成功,進(jìn)行其他操作'); }else{ $scope.reset_pwd.submitted = true; } }
親測(cè)可用。
第二種失去焦點(diǎn)驗(yàn)證
<form action="" class="form-horizontal col-md-9" name="reset_pwd" ng-submit="resetPwd()"> <div class="form-group"> <label class="col-sm-2 control-label">密  碼</label> <div class="col-sm-8"> <input type="password" name="mycompwd" class="form-control" ng-model="mycompwd" placeholder="請(qǐng)輸入密碼"> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">重復(fù)密碼</label> <div class="col-sm-8"> <input ng-focus type="password" name="resetmycompwd" class="form-control" ng-model="resetmycompwd" placeholder="再次輸入密碼" required> </div> <span style="color:red" ng-show="mycompwd!=resetmycompwd && reset_pwd.resetmycompwd.$dirty && !reset_pwd.resetmycompwd.$focused ">密碼不一致</span> </div> <div class="form-group"> <button type="submit" class="btn btn-primary" >確認(rèn)</button> <button type="button" class="btn btn-default" ng-click="resetpwd_cancle()">取消</button> </div> </form>
JS代碼
app.directive('ngFocus',[function(){ var focusClass = 'ng-focused'; return{ restrict:'AE', require:'ngModel', link:function(scope,element,attrs,ctrl){ ctrl.$focused = false; element.bind('focus',function(e){ element.addClass(focusClass); scope.$apply(function(){ ctrl.$focused = true; }); element.bind('blur',function(e){ element.removeClass(focusClass); scope.$apply(function(){ ctrl.$focused = false; }); }); }) } }; }]);
注意HTML標(biāo)紅的地方。正是區(qū)分兩種方法的關(guān)鍵。
以上所述是小編給大家介紹的Angualrjs 表單驗(yàn)證的兩種方式(失去焦點(diǎn)驗(yàn)證和點(diǎn)擊提交驗(yàn)證),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Angular-UI Bootstrap組件實(shí)現(xiàn)警報(bào)功能
這篇文章主要介紹了Angular-UI Bootstrap組件實(shí)現(xiàn)警報(bào)功能,對(duì)Angular.js services的學(xué)習(xí)有所幫助,需要的朋友可以參考下2018-07-07淺談關(guān)于angularJs中使用$.ajax的注意點(diǎn)
本篇文章主要介紹了關(guān)于angularJs中使用$.ajax的注意點(diǎn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08對(duì)angularJs中$sce服務(wù)安全顯示html文本的實(shí)例
今天小編就為大家分享一篇對(duì)angularJs中$sce服務(wù)安全顯示html文本的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09使用AngularJS創(chuàng)建單頁應(yīng)用的編程指引
這篇文章主要介紹了使用AngularJS創(chuàng)建單頁應(yīng)用的編程指引,AngularJS是一款高人氣的JavaScript庫,需要的朋友可以參考下2015-06-06Angularjs實(shí)現(xiàn)多圖片上傳預(yù)覽功能
這篇文章主要介紹了Angularjs實(shí)現(xiàn)多圖片上傳預(yù)覽功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Angular4自制一個(gè)市縣二級(jí)聯(lián)動(dòng)組件示例
本篇文章主要介紹了Angular4自制一個(gè)市縣二級(jí)聯(lián)動(dòng)組件示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11angularJs在多個(gè)控制器中共享服務(wù)數(shù)據(jù)的方法
今天小編就為大家分享一篇angularJs在多個(gè)控制器中共享服務(wù)數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09angularjs實(shí)現(xiàn)過濾并替換關(guān)鍵字小功能
這篇文章主要為大家詳細(xì)介紹了angularjs實(shí)現(xiàn)過濾并替換關(guān)鍵字小功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09