Skip to content

Instantly share code, notes, and snippets.

@b4dnewz
Created July 6, 2017 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b4dnewz/403b8fd67f901d3e51fe177c56b84830 to your computer and use it in GitHub Desktop.
Save b4dnewz/403b8fd67f901d3e51fe177c56b84830 to your computer and use it in GitHub Desktop.
Angular directive to validate input based on minimum words length.
'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