var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
});
app.directive('noDogs', function factory(){
return {
require: '?ngModel',
link: function(scope, element, attr, ctrl){
if(ctrl){
ctrl.$parsers.push(function(value){
if(/dog/.test(value)){
ctrl.$setValidity("noDogs", false);
return undefined;
} else {
ctrl.$setValidity("noDogs", true);
return value;
}
})
}
}
}
})
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<link data-require="bootstrap-css@3.1.1" data-semver="3.1.1" rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.2.x" src="https://code.angularjs.org/1.2.16/angular.js" data-semver="1.2.16"></script>
<script data-require="jquery@*" data-semver="2.0.3" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script data-require="bootstrap@*" data-semver="3.1.1" src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl" class="container">
<div class="page-header">
<h3>Custom validation <small>Angular forms</small></h3>
</div>
<form role="form" name="demoForm">
<div class="form-group" ng-class="{'has-error': !demoForm.mail.$valid}">
<input type="email" ng-model="email" name="mail" no-dogs class="form-control" placeholder="Enter email" required>
<span class="help-block" ng-show="demoForm.mail.$error.email">Please enter a valid email address</span>
<span class="help-block" ng-show="demoForm.mail.$error.noDogs">No dogs allowed!</span>
</div>
<div class="form-group" ng-class="{'has-error': !demoForm.password.$valid}">
<input type="password" ng-model="password" class="form-control" placeholder="Password" required ng-minlength="8" name="password">
<span class="help-block" ng-show="demoForm.password.$error.minlength">Password must be at least 8 chars long</span>
</div>
<button type="submit" class="btn btn-primary">Register</button>
</form>
</body>
</html>
/* Put your css in here */