這篇文章主要介紹Angualrjs中表單驗證的方式有哪些,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
AngularJS提供了表單驗證,但是驗證的過程交互體驗很不好,比如重設(shè)密碼,重復(fù)密碼的時候一鍵入就會提示密碼不正確,現(xià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="請輸入密碼"> </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 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)用戶試圖提交表單時,你可以在作用域中捕獲到一個submitted值,然后對表單內(nèi)容進行驗證并顯示錯誤信息。
JS代碼
$scope.submitted = false; $scope.resetPwd = function(){ console.log(666); if($scope.reset_pwd.$valid && $scope.mycompwd == $scope.resetmycompwd){ console.log('重置成功,進行其他操作'); }else{ $scope.reset_pwd.submitted = true; } }
親測可用。
第二種失去焦點驗證
<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="請輸入密碼"> </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 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; }); }); }) } }; }]);
以上是“Angualrjs中表單驗證的方式有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.rwnh.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
當(dāng)前題目:Angualrjs中表單驗證的方式有哪些-創(chuàng)新互聯(lián)
URL網(wǎng)址:http://www.rwnh.cn/article4/ddddoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、品牌網(wǎng)站制作、企業(yè)建站、小程序開發(fā)、定制開發(fā)、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容