国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Angualrjs 表單驗證的兩種方式(失去焦點驗證和點擊提交驗證)

2019-11-19 16:37:54
字體:
來源:轉載
供稿:網友

AngularJS提供了表單驗證,但是驗證的過程交互體驗很不好,比如重設密碼,重復密碼的時候一鍵入就會提示密碼不正確,現整理了兩種方法,僅供借鑒。

一,點擊提交驗證

<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">重復密碼</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" >確認</button>    <button type="button" class="btn btn-default" ng-click="resetpwd_cancle()">取消</button>  </div></form>

當用戶試圖提交表單時,你可以在作用域中捕獲到一個submitted值,然后對表單內容進行驗證并顯示錯誤信息。

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">重復密碼</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" >確認</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標紅的地方。正是區分兩種方法的關鍵。

以上所述是小編給大家介紹的Angualrjs 表單驗證的兩種方式(失去焦點驗證和點擊提交驗證),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 开鲁县| 扎赉特旗| 台东市| 淮安市| 修武县| 弥渡县| 长沙县| 苍梧县| 凤城市| 思茅市| 军事| 兰坪| 琼结县| 库尔勒市| 吴江市| 延津县| 玉田县| 夏河县| 弥勒县| 吉林市| 巴楚县| 新津县| 齐齐哈尔市| 邛崃市| 仲巴县| 正蓝旗| 宁南县| 侯马市| 湖口县| 达州市| 政和县| 吉水县| 鹰潭市| 三河市| 保定市| 城固县| 韶关市| 甘泉县| 卓资县| 武邑县| 海原县|