Skip to content

Instantly share code, notes, and snippets.

@hyrmn
Created October 5, 2013 20:19
Show Gist options
  • Select an option

  • Save hyrmn/6845573 to your computer and use it in GitHub Desktop.

Select an option

Save hyrmn/6845573 to your computer and use it in GitHub Desktop.
Custom validation with popover
angular.module('profile.changePwd.pwdValidate', [])
.directive('pwdValidate', function () {
return {
require: 'ngModel',
restrict: 'A',
template: '<input data-placement="right" bs-popover="passwordRules" data-hide="hidePopup" data-trigger="focus">',
replace: true,
transclude: 'element',
link: function (scope, element, attrs, ctrl) {
ctrl.$parsers.unshift(function (viewValue) {
scope.pwdValidLength = viewValue && viewValue.length >= 8 ? true : false;
scope.pwdHasUpperLetter = (viewValue && /[A-Z]/.test(viewValue)) ? true : false;
scope.pwdHasLowerLetter = (viewValue && /[A-z]/.test(viewValue)) ? true : false;
scope.pwdHasNumber = (viewValue && /\d/.test(viewValue)) ? true : false;
if (scope.pwdValidLength && scope.pwdHasUpperLetter && scope.pwdHasLowerLetter && scope.pwdHasNumber) {
ctrl.$setValidity('pwd', true);
} else {
ctrl.$setValidity('pwd', false);
}
return viewValue;
});
scope.getColor = function (isValid) {
return isValid ? 'text-success' : 'text-error';
};
scope.getIcon = function (isValid) {
return isValid ? 'icon-ok' : 'icon-remove';
};
scope.passwordRules = {
"content": "Your password must meet the following requirments:<br />" +
"<ul class='pwdRules'>" +
"<li ng-class='getColor(pwdHasUpperLetter)'><i ng-class='getIcon(pwdHasUpperLetter)'></i> At least <strong>one uppercase letter</strong>" +
"<li ng-class='getColor(pwdHasLowerLetter)'><i ng-class='getIcon(pwdHasLowerLetter)'></i> At least <strong>one lowercase letter</strong>" +
"<li ng-class='getColor(pwdHasNumber)'><i ng-class='getIcon(pwdHasNumber)'></i> At least <strong>one number</strong>" +
"<li ng-class='getColor(pwdValidLength)'><i ng-class='getIcon(pwdValidLength)'></i> At least <strong>8 characters long</strong>" +
"</ul>"
};
}
};
});
<div class="control-group" ng-class="{error: editForm.Password.$dirty && editForm.Password.$invalid}">
<label class="control-label" for="Password" title="Password">Password</label>
<div class="controls">
<input type="password" id="Password" name="Password" ng-model="changePwd.Password" placeholder="Password" class="input-medium" required pwd-validate>
<span ng-show="editForm.Password.$dirty && editForm.Password.$error.required" class="help-inline">Please enter a new password.</span>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment