Skip to content

Instantly share code, notes, and snippets.

@pbakondy
Last active August 29, 2015 14:09
Show Gist options
  • Save pbakondy/c73bb271d3eb56422255 to your computer and use it in GitHub Desktop.
Save pbakondy/c73bb271d3eb56422255 to your computer and use it in GitHub Desktop.
Angular directive to check consecutive characters
var app = angular.module('myApp', []);
app.directive('consecutive', function () {
return {
require: 'ngModel',
restrict: 'A',
priority: 50,
link: function(scope, elem, attrs, ctrl) {
if (!ctrl) return;
// four identical consecutive characters
var CONSECUTIVE_REGEX = new RegExp('(.)\\1\\1\\1+');
function checkConsecutive(value) {
if (ctrl.$isEmpty(value)) {
ctrl.$setValidity('consecutive', true);
} else {
var valid = !(CONSECUTIVE_REGEX.test(value));
ctrl.$setValidity('consecutive', valid);
}
}
ctrl.$parsers.push(checkConsecutive);
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment