Shekspir
Shekspir

Reputation: 89

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.

Upvotes: 1

Views: 3159

Answers (1)

eladcon
eladcon

Reputation: 5825

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;
}

Upvotes: 8

Related Questions