Search code examples
javascriptangularjsvalidationangularjs-directiveng-tags-input

How to do custom validation in ngTagsInput(not regex)?


For example I want to add custom validation where addedValue<20, how to implemenet it?

<tags-input ng-model="numbers"
        placeholder="Add a number"
        min-length="1"
        max-length="3"
        allowed-tags-pattern="^[0-9]+$"
        onTagAdded="$tag<20"></tags-input>

this is code I use.


Solution

  • You could use the on-tag-adding attribute to check if it's a valid tag:

    <tags-input ng-model="numbers" 
                placeholder="Add a number" 
                min-length="1"
                max-length="3"
                on-tag-adding="checkTag($tag)">
    </tags-input>
    

    In your controller:

    $scope.checkTag = function(tag) {
        // check if its a valid number and smaller than 20
        return !isNaN(tag.text) && +tag.text < 20;
    }