Last active
August 29, 2015 14:09
-
-
Save pbakondy/c73bb271d3eb56422255 to your computer and use it in GitHub Desktop.
Angular directive to check consecutive characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode 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