238

Suppose we're building an address book application (contrived example) with AngularJS.

We have a form for contacts that has inputs for email and phone number, and we want to require one or the other, but not both: We only want the email input to be required if the phone input is empty or invalid, and vice versa.

Angular has a required directive, but it's not clear from the documentation how to use it in this case. So how can we conditionally require a form field? Write a custom directive?

6 Answers 6

473

There's no need to write a custom directive. Angular's documentation is good but not complete. In fact, there is a directive called ngRequired, that takes an Angular expression.

<input type='email'
       name='email'
       ng-model='contact.email' 
       placeholder='[email protected]'
       ng-required='!contact.phone' />

<input type='text'
       ng-model='contact.phone'             
       placeholder='(xxx) xxx-xxxx'
       ng-required='!contact.email' />  

Here's a more complete example: http://jsfiddle.net/uptnx/1/

Sign up to request clarification or add additional context in comments.

3 Comments

It shouldn't be a problem, right? Just update the conditionals accordingly. If you explain what you need a bit more I (or other person here) will be able to show you :)
As a side note, you can also use a function and do more complex logic there.
This feature is documented by now: docs.angularjs.org/api/ng/directive/ngRequired
26

if you want put a input required if other is written:

   <input type='text'
   name='name'
   ng-model='person.name'/>

   <input type='text'
   ng-model='person.lastname'             
   ng-required='person.name' />  

Regards.

Comments

15

For Angular2

<input type='email' 
    [(ngModel)]='contact.email'
    [required]='!contact.phone' >

4 Comments

EDIT: This is throwing a console error "ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked." when I check the radio button I'm applying this on, but it does appear to do the conditional validation.
Angular2+ isn't tagged on this question. Since it's essentially a different framework, this answer isn't relevant.
@isherwood you are right. I added it because I ended here while googling for that. I will delete it if it gets downvoted or controversial.
@Iaffuste you could post and reply on a new question for Angular2+ to make it easy for people looking into this. +1 anyway =)
14

Simple you can use angular validation like :

 <input type='text'
   name='name'
   ng-model='person.name'
   ng-required='!person.lastname'/>

   <input type='text'
   name='lastname'
   ng-model='person.lastname'             
   ng-required='!person.name' /> 

You can now fill the value in only one text field. Either you can fill name or lastname. In this way you can use conditional required fill in AngularJs.

2 Comments

One can still fill in both the fields, can't they?
yes user can fill both the field, also in this condition if user fill one field, the other field is not mandatory
0

In AngularJS (version 1.x), there is a build-in directive ngRequired

<input type='email'
       name='email'
       ng-model='user.email' 
       placeholder='[email protected]'
       ng-required='!user.phone' />

<input type='text'
       ng-model='user.phone'             
       placeholder='(xxx) xxx-xxxx'
       ng-required='!user.email' /> 

In Angular2 or above

<input type='email'
       name='email'
       [(ngModel)]='user.email' 
       placeholder='[email protected]'
       [required]='!user.phone' />

<input type='text'
       [(ngModel)]='user.phone'             
       placeholder='(xxx) xxx-xxxx'
       [required]='!user.email' /> 

Comments

0

For Angular 2

<input [(ngModel)]='email' [required]='!phone' />
<input [(ngModel)]='phone' [required]='!email' /> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.