<!DOCTYPE html><html ng-app="app"><head><meta charset="UTF-8"><title>form1</title><link rel="stylesheet" type="text/css" href="static/plugins/bootstrap.min.css"><script type="text/javascript" src="static/plugins/angular.min.js"></script><script type="text/javascript" src="static/plugins/angular-messages.js"></script></head><body><div class="col-md-6"><form role="form" name="myForm" class="form-horizontal" novalidate><label>用户名</label><input type="email" placeholder="ng-Messages测试" name="name" ng-model="username.name"ng-minlength=3 ng-maxlength=20 required /><div ng-messages="myForm.name.$error"><div ng-message="required">必填项</div><div ng-message="minlength">字符太短小于3</div><div ng-message="maxlength">字符太长大于20</div><div ng-message="email">正确的邮箱格式</div></div> </form></div></body><script type="text/javascript">angular.module("app", ["ngMessages"]);</script></html>这样就可以了,有几个点要声明一下,
<input type="text" placeholder="Desired username" name="username" ng-model="signup.username" ng-minlength=3 ng-maxlength=20 ensure-unique="username" required />你看这个ensure-unique就是自定义的验证,就是需要独一无二呗,这就是在html上的写法,自定义的js代码就是自己写啦,这里也有一个这个对应的代码,是用指令写的,恩上代码
angular.module("myApp", []).directive("ensureUnique", ["$http", function($http) {return {require: "ngModel",link: function(scope, ele, attrs, c) {scope.$watch(attrs.ngModel, function() {$http({method: "POST",url: "/api/check/" + attrs.ensureUnique,data: {"field": attrs.ensureUnique}}).success(function(data, status, headers, cfg) {c.$setValidity("unique", data.isUnique);}).error(function(data, status, headers, cfg) {c.$setValidity("unique", false);});});}};}]);看到没,html,js两步完成自定义验证,就是自定义验证我感觉有点我有点low啊,搞不太来。反正我贴这两段代码就是告诉你们自定义验证的写法是咋样的,哈哈
然后呢,这个怎么用呢,好的,上代码
<!DOCTYPE html><html><head><meta charset="utf-8"><script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script></head><body><h2>验证实例</h2><form ng-app="myApp" ng-controller="validateCtrl"name="myForm" novalidate><p>用户名:<br><input type="text" name="user" ng-model="user" required><span style="color:red" ng-show="myForm.user.$dirty && myForm.user.$invalid"><span ng-show="myForm.user.$error.required">用户名是必须的。</span></span></p><p>邮箱:<br><input type="email" name="email" ng-model="email" required><span style="color:red" ng-show="myForm.email.$dirty && myForm.email.$invalid"><span ng-show="myForm.email.$error.required">邮箱是必须的。</span><span ng-show="myForm.email.$error.email">非法的邮箱地址。</span></span></p><p><input type="submit"ng-disabled="myForm.user.$dirty && myForm.user.$invalid || myForm.email.$dirty && myForm.email.$invalid"></p></form><script>var app = angular.module("myApp", []);app.controller("validateCtrl", function($scope) {$scope.user = "John Doe";$scope.email = "john.doe@gmail.com";});</script></body></html>这个的用法总结几点,
<!DOCTYPE html><html ng-app="app"><head><meta charset="UTF-8"><title>form1</title><link rel="stylesheet" type="text/css" href="static/plugins/bootstrap.min.css"><script type="text/javascript" src="static/plugins/angular.min.js"></script><script type="text/javascript" src="static/plugins/angular-messages.js"></script></head><body ng-controller="ctrl"><div class="col-md-6"><form role="form" name="myForm" class="form-horizontal" novalidate><label>用户名</label><input type="email" placeholder="ng-Messages测试" name="name" ng-model="username.name"ng-minlength=3 ng-maxlength=20 required /><div ng-messages="myForm.name.$error"><div ng-message="required">必填项</div><div ng-message="minlength">字符太短小于3</div><div ng-message="maxlength">字符太长大于20</div><div ng-message="email">正确的邮箱格式</div></div>名字 <input type="text" name="name1" ng-model="name1" required><span style="color:red" ng-show="myForm.name1.$dirty"><span ng-show="myForm.name1.$error.required">名字是必须的</span></span></form></div></body><script type="text/javascript">var app=angular.module("app", ["ngMessages"]);app.controller("ctrl",function($scope){$scope.name1="wenwen";})</script></html>就是这样子。