Shimon Brandsdorfer
Shimon Brandsdorfer

Reputation: 1723

ng form depended validation on another field

I have a angular form, and I have there two input-fields, one for form and one for email.

I Want to require email OR phone, How do I do it?

I tried to do : ng-reqired="myForm.phone.$invalid", but this doesn't seem to work, once you put in one of the fields (and both became valid), so even after you erase the input, both inputs are still valid, (because once the phone became valid, the email became valid.

here is my JSfiddle: Js Fiddle

I have a short-term solution by doing ng-required="myForm.email.$viewValue == ''", but i'm looking for a better one.

because this will only check if the other field is empty, and I do want to check if it's valid, for example, when the user a non-valid email address, should the phone be required.

Upvotes: 0

Views: 64

Answers (1)

Icycool
Icycool

Reputation: 7179

You can use ng-required="!form.phone && !form.email" for both text fields.

It means when both model is empty, required is set for that input, else unset it.

Upvotes: 1

Related Questions