AngularJS表單驗證中級篇(3)
目錄
基本驗證
驗證插件messages
自定義驗證
基本驗證
<form name="form" novalidate ng-app> <span>{{form.$invalid}}</span> <span>{{form.$valid}}</span> <span>{{form.$dirty}}</span> <span>{{form.$pristine}}</span> <input type="text" ng-model="user" required /> <input type="text" ng-model="pwd" required minlength="4" ng-maxlength="5" /> <input type="text" ng-model="phone" required ng-pattern="/1[3|5|7|8|][0-9]{9}/" /> <input type="email" ng-model="email" required /> <input type="url" ng-model="url" required /> <input type="number" ng-model="number" required /> <div> <button type="reset" ng-disabled="form.$pristine">重置</button> <button type="submit" ng-disabled="form.$invalid">提交</button> </div> </form>
以上展示了基本的ng驗證.
這里重點介紹一下上面的特例:
novalidate:禁用H5自帶的驗證
ng-maxlength: 如果不寫ng,maxlength則直接限制最多輸入字符,稍微有點區(qū)別(IE9 + Chrome 測試)
ng-pattern:通過正則驗證,如果不寫ng開頭,無驗證效果.
注:要啟用驗證 同時需要綁定一個ng-model
訪問表單屬性
---方位表單: <form name>.<angular property>
---訪問一個輸入框: <form name>.<input name>.<angular property>
驗證插件
在介紹messages插件之前,我們看下本來的驗證提示
<form name="form" ng-app novalidate> <span>{{form.user.$error.required?'user該項必填':''}}</span> <input type="text" ng-model="user" name="user" required /> <span>{{form.pwd.$error.required?'pwd該項必填':''}}</span> <input type="text" ng-model="pwd" name="pwd" required /> <span>{{form.info.$error.required?'info該項必填':''}}</span> <input type="text" ng-model="info" name="info" required /> <span>{{form.age.$error.required?'age該項必填':''}}</span> <input type="text" ng-model="age" name="age" required /> <div> <button type="submit" ng-disabled="form.$invalid">提交</button> </div> </form>
這里只是判斷了require 當(dāng)我們的代碼 我們重復(fù)寫了很多3元表達(dá)式
messages插件就是更友好的解決重復(fù)的問題
<form name="form" ng-app="myApp" novalidate> <input type="email" ng-model="user" name="username" required minlength="4" /> <div ng-messages="form.username.$error" ng-messages-multiple> <div ng-message="required">該項必填</div> <div ng-message="minlength">低于最低長度</div> <div ng-message="email">應(yīng)為email</div> </div> </form> <script src="Scripts/angular.min.js"></script> <script src="Scripts/angular-messages.min.js"></script> <script> angular.module('myApp', ['ngMessages']); </script>
Nuget:Install-Package AngularJS.Messages
自定義驗證
通過基本的驗證方式,我們已經(jīng)能夠解決大部分的驗證問題.但項目中永遠(yuǎn)充滿著各種各樣的需求.
在ng中的自定義驗證,一般通過指令的形式創(chuàng)建.
<form name="form" ng-app="myApp" novalidate> <input type="email" ng-model="user" name="username" required ensure-unique minlength="4" /> <div ng-messages="form.username.$error" ng-messages-multiple> <div ng-message="required">該項必填</div> <div ng-message="minlength">低于最低長度</div> <div ng-message="email">應(yīng)為email</div> <div ng-message="unique">用戶名已存在</div> </div> </form>
在上面的messages插件Demo中,新建一行驗證用戶名已存在 以及 在input上添加了ensure-unique指令
同時,我們需要在js中定義ensure-unique指令:
angular.module('myApp', ['ngMessages']).directive('ensureUnique', ['$http', '$timeout', '$window', function ($http, $timeout, $window) { return { restrict: "A", require: 'ngModel', link: function (scope, ele, attrs, ngModelController) { scope.$watch(attrs.ngModel, function (n) { if (!n) return; $timeout.cancel($window.timer); $window.timer = $timeout(function () { $http({ method: 'get', url: '/api/checkusername/', //根據(jù)換成自己的url params: { "username": n } }).success(function (data) { ngModelController.$setValidity('unique', data.isUnique); //這個取決于你返回的,其實就是返回一個是否正確的字段,具體的這塊可以自己修改根據(jù)自己的項目 }).error(function (data) { ngModelController.$setValidity('unique', false); }); }, 500); }); } }; }]);
指令不是本節(jié)重點內(nèi)容,這里簡單說下
ngModelController.$setValidity('unique', bool);
通過該API可以設(shè)置$error.unique.
setValidity為true,則$error.unique為false
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
AngularJS ng-repeat指令中使用track by子語句解決重復(fù)數(shù)據(jù)遍歷錯誤問題
這篇文章主要介紹了AngularJS ng-repeat指令中使用track by子語句解決重復(fù)數(shù)據(jù)遍歷錯誤問題,結(jié)合實例形式分析了ng-repeat指令遍歷JavaScript數(shù)組錯誤的原因與相關(guān)解決技巧,需要的朋友可以參考下2017-01-01angularjs+bootstrap實現(xiàn)自定義分頁的實例代碼
本篇文章主要介紹了angularjs+bootstrap實現(xiàn)自定義分頁的實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06AngularJS基礎(chǔ) ng-keydown 指令簡單示例
本文主要介紹AngularJS ng-keydown 指令,在這里幫大家整理了ng-keydown 指令的基礎(chǔ)資料,并附有代碼,有需要的朋友可以參考下2016-08-08angular2 ng build部署后base文件路徑問題詳細(xì)解答
本篇文章主要介紹了angular2 ng build部署后base文件路徑問題詳細(xì)解答,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07