Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

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?

1 Answer

0 votes
by (106k points)
edited by

For conditionally require form inputs with AngularJS, you no need to write a custom directive. Angular's documentation is good but not complete. In fact, there is a directive and ngRequired

Are you interested in learning Angularjs from the basics! Here's the right video for you on Angularjs provided by Intellipaat:

that takes an Angular expression. See the code below:-

<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' />

Related questions

+2 votes
1 answer
0 votes
2 answers
asked Sep 2, 2019 in Web Technology by Tech4ever (20.3k points)
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...