Created
July 6, 2017 14:17
-
-
Save b4dnewz/403b8fd67f901d3e51fe177c56b84830 to your computer and use it in GitHub Desktop.
Angular directive to validate input based on minimum words length.
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
'use strict' | |
###* | |
# @ngdoc directive | |
# @name app.directive:ngMinwords | |
# @description Angular directive to validate input based on minimum words length. | |
# # ngMinwords | |
### | |
angular.module 'app' | |
.directive 'ngMinwords', -> | |
restrict: 'A' | |
require: '?ngModel' | |
link: (scope, element, attrs, ctrl) -> | |
if (!ctrl) | |
return | |
minwords = 0 | |
attrs.$observe('ngMinwords', (value) -> | |
minwords = parseInt(value) || 0 | |
ctrl.$validate() | |
) | |
ctrl.$validators.minwords = (modelValue, viewValue) -> | |
ctrl.$isEmpty(viewValue) || viewValue.match(/\S+/g).length >= minwords |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment