Skip to content

Repository files navigation

angular-validate-directive

An angular directive for doing one-off custom validation.

Under the hood this directive uses the validator API introduced in angular 1.3. See NgModelController's $validators and $asyncValidators properties:

https://docs.angularjs.org/api/ng/type/ngModel.NgModelController

Usage

  1. Include validate.js on your page after angular.js
  2. Add dbrans.validate as a dependency of your app.

Synchronous validation

<formname="myForm"><inputname="foo" ng-model="foo" dbrans-validate="{odd: isOdd}"><divng-messages="myForm.foo.$error"><divng-message="odd">You need to pick an odd number.</div></div></form>
angular.module('yourModule').controller('MyController',function($scope){$scope.isOdd=function(x){returnx%2===1;};});

Async validation

<formname="myForm">
Choose a unique username: <inputname="username" ng-model="username" dbrans-validate-async="{unique: isUsernameUnique}"><divng-messages="myForm.username.$error"><divng-message="unique">Sorry, that username is taken.</div></div></form>
angular.module('yourModule').controller('MyController',function($scope,$http,$q){$scope.isUsernameUnique=function(username){$http.get('/api/user/'+username).then(function(){return$q.reject();// 200 - user exists},function(){returntrue;// 404 - user does not exist});};});

About

An angular directive enabling light-weight validation.

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages