1:判断前后密码是否一致
<html ng-app='app'> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.4/angular.js"></script> <script> angular.module('app', []) .directive('pwCheck', [function () { return { require: 'ngModel', link: function (scope, elem, attrs, ctrl) { var firstPassword = '#' + attrs.pwCheck; elem.add(firstPassword).on('keyup', function () { var v = elem.val()===$(firstPassword).val(); console.log(v); ctrl.$setValidity('pwcheck', v); }); } } }]); </script> <form name="form"> <input type="text" id="pw1" name="pw1" ng-model="pw1Model"> <input type="text" name="pw2" pw-check="pw1" ng-model="pw2Model"> Valid: {{form.pw2.$valid}} <pre>{{form.pw2 |json}}</pre> </form>
:2:用户名是否存在
.directive('pwdCheck', ['$http','httpLoadData',function($http,httpLoadData) { return { require: 'ngModel', link: function (scope, elem, attrs, ctrl) { elem.on('keyup', function () { var obj= { "pwd":attrs.pwdCheck }; var params ={ requestParams:encodeURIComponent(JSON.stringify(obj)) // 参数需要 URL 编码s }; $http({ method: 'get', url:httpLoadData.setUrl+'/sf/service/user/check/password/rules', //根据换成自己的url params:params }).success(function(data) { var hide_show=false; if(data.length!=0){ hide_show=true; } console.log(obj) console.log( data) ctrl.$setValidity('pwdcheck', hide_show); }).error(function(msg) { alert(msg) ngModelController.$setValidity('unique', false); }); }); } } } ])
<input user-check="{{dataForm.login}}" type="text" class="form-control" name="login" ng-model="dataForm.login" ng-value="{{dataForm.login}}" /> <div class="error" ng-show="adduserForm.login.$dirty &&adduserForm.login.$invalid"> <span ng-show="adduserForm.login.$valid==false"> The username already exists!</span> </div>