Angualrjs 表單驗證的兩種方式(失去焦點驗證和點擊提交驗證)
AngularJS提供了表單驗證,但是驗證的過程交互體驗很不好,比如重設密碼,重復密碼的時候一鍵入就會提示密碼不正確,現(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">重復密碼</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值,然后對表單內(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">重復密碼</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標紅的地方。正是區(qū)分兩種方法的關鍵。
以上所述是小編給大家介紹的Angualrjs 表單驗證的兩種方式(失去焦點驗證和點擊提交驗證),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
Angularjs中$http以post請求通過消息體傳遞參數(shù)的實現(xiàn)方法
這篇文章主要介紹了Angularjs中$http以post請求通過消息體傳遞參數(shù)的方法,結(jié)合實例形式分析了$http使用post請求傳遞參數(shù)的相關設置與使用技巧,需要的朋友可以參考下2016-08-08
Angular事件之不同組件間傳遞數(shù)據(jù)的方法
這篇文章主要介紹了Angular事件之不同組件間傳遞數(shù)據(jù)的方法,利用Angular Event在不同組件之間傳遞數(shù)據(jù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11
Angular跨字段驗證器中如何直接調(diào)用其它獨立的驗證器
我們在開發(fā)的時候都會用到表單,那么驗證器就是必不可少的東西,這篇文章主要給大家介紹了關于在Angular跨字段驗證器中如何直接調(diào)用其它獨立的驗證器的相關資料,需要的朋友可以參考下2022-03-03
AngularJs Dependency Injection(DI,依賴注入)
本文主要介紹AngularJs Dependency Injection,這里整理了詳細資料及示例代碼有興趣的小伙伴可以參考下2016-09-09
angular2 ng2-file-upload上傳示例代碼
這篇文章主要介紹了angular2 ng2-file-upload上傳示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08

