Link to home
Start Free TrialLog in
Avatar of Sandy V
Sandy V

asked on

Validations in angularJS

Is there a way to set two fields combined as required field in AngularJS, let's say you have two text fields Product & Order & i need to make sure that atleast one of the field is populated
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Take a look at the ng-required directive https://docs.angularjs.org/api/ng/directive/ngRequired
<input ng-required="expression">

Open in new window


where expression is an expression that checks your model values

<input ng-required="!(model.Product.length || model.Order.length)">

Open in new window


In your controller code
...
 $scope.model = {
    Product: '',
    Order: '',
    ...
 }

Open in new window

Avatar of Sandy V
Sandy V

ASKER

Here Product & Order are 2 different input fields. Where should i put the ng-required directive?? i thought the only way to achieve this is using a custom directive
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sandy V

ASKER

Perfect, thanks for the explanation
You are welcome.