1

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

2
  • 2
    using ng-change directive, call a method and pass the value. You can compare there and do you stuff based on that. Commented Oct 21, 2015 at 12:57
  • If you want it tied into the form validation you need to use a directive with a custom ngModel validator in it Commented Oct 21, 2015 at 13:11

1 Answer 1

1

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'">
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.