Skip to content

Instantly share code, notes, and snippets.

@dting
Created November 7, 2015 17:57
Show Gist options
  • Save dting/0eaf5edc5a2b713796b1 to your computer and use it in GitHub Desktop.
Save dting/0eaf5edc5a2b713796b1 to your computer and use it in GitHub Desktop.
<!-- begin snippet: js hide: false -->
<!-- language: lang-js -->
var app = angular.module('app', ['ngMessages']);
app.controller('myController', function($scope) {
$scope.data = {};
});
app.directive('passwordFormatValidator', function() {
return {
require : 'ngModel',
link : function(scope, element, attrs, ngModel) {
ngModel.$parsers.push(function(value) {
if(!value || value.length == 0) return;
ngModel.$setValidity('digit', /[0-9]/.test(value));
ngModel.$setValidity('case', /[A-Z]/.test(value) && /[a-z]/.test(value));
ngModel.$setValidity('special', /[^a-z0-9 ]/i.test(value));
return value;
});
}
};
});
<!-- language: lang-css -->
.errors {
color: red;
font-size: 0.75em;
}
<!-- language: lang-html -->
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular-messages.js"></script>
<div ng-app='app' ng-controller="myController">
<form name="form" novalidation>
<div class="field">
<label for="password">Enter new password:</label>
<input type="password"
name="password"
ng-model="data.password"
ng-minlength="5"
ng-maxlength="30"
password-format-validator
required />
<ng-messages for="form.password.$error" ng-show="form.password.$touched" multiple class="errors">
<div ng-message="required">Password is required</div>
<div ng-message-exp="['minlength', 'maxlength']">Password must be between 5 and 30 characters in length</div>
<div ng-message="digit">Password must include a digit</div>
<div ng-message="case">Password must include both upper and lower case characters</div>
<div ng-message="special">Password must include a special character</div>
</ng-messages>
</div>
</form>
</div>
<!-- end snippet -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment