leopik
leopik

Reputation: 2351

Validating input based on array

I need to validate an input so that it doesn't contain a string that also appears in a given array of strings.

To explain it using a simple example, the validation should fail when I type town, city or house. If there is any other string, it should pass.

<input type="text" ng-not-in="r.arr" ...

r.arr = ["town", "city", "house"]

I am aware of possibilities such as using $watch, writing my own directive or ng-pattern (well, for this case it's not suitable) however, is there something built in that would do this? If not, which approach should I choose, which is the "cleanest"?

Thanks

Upvotes: 1

Views: 59

Answers (1)

hoeni
hoeni

Reputation: 3280

You could use ui-validate from Angular UI. That way you don't have to write an own directive everytime, but simply use an expression (See JSfiddle):

    <input type="text" ng-model="mod.text" ui-validate="'list.indexOf($value)==-1'">

Upvotes: 1

Related Questions