0

I want to a validation on a form in the angular way. I have three input boxes. All three are required when one of them is entered. Otherwise they are not required. Continue button must be enabled only when all three are entered or not enetered.

You can review the code on this plunker. Here's a snippet, too, just in case plunker is inconvenient:

angular.module('test', []).config(function() {});

angular.module('test').controller('testctrl', function($rootScope, $scope, $location) {
  $scope.admin = [{
    "number": "2"
  }];
  $scope.adminInfos = {};
  $scope.validations = {};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<form name="test_form">
  <input type="text" name="admin{{admin.number}}_firstname" ng-model="adminInfos[admin.number].firstname" placeholder="First Name" ng-required="adminInfos[admin.number].lastname.length && adminInfos[admin.number].email.length" />
  <span ng-show="!system_admins.admin{{admin.number}}_firstname.$pristine && (adminInfos[admin.number].email.length || adminInfos[admin.number].lastname.length) && !adminInfos[admin.number].firstname.length" class="error">First name is required</span>
  <br/>
  <input type="text" name="admin{{admin.number}}_lastname" ng-model="adminInfos[admin.number].lastname" placeholder="Last Name" ng-required="adminInfos[admin.number].firstname.length && adminInfos[admin.number].email.length" />
  <span ng-show="!system_admins.admin{{admin.number}}_lastname.$pristine && (adminInfos[admin.number].email.length ||  adminInfos[admin.number].firstname.length ) && !adminInfos[admin.number].lastname.length" class="error">Last name is required</span>
  <br/>
  <input type="text" name="admin{{admin.number}}_email" ng-model="adminInfos[admin.number].email" placeholder="Email Address" ng-required="adminInfos[admin.number].firstname.length && adminInfos[admin.number].lastname.length" />
  <span ng-show="!system_admins.admin{{admin.number}}_email.$pristine && !adminInfos[admin.number].email.length && (adminInfos[admin.number].lastname.length || adminInfos[admin.number].firstname.length)" class="error">Email is required</span>

  <br/>
  <br/>

  <button ng-disabled="test_form.$invalid">Continue</button>

</form>

I have an issue where continue button is still enabled if I enter the first name and leave the last name and email as blank.

Continue button must be enabled if all three are not entered or all three are entered.

3 Answers 3

1

Your ng-required condition is wrong, change your ng-required condition to use or (||) instead of & for all fields

 ng-required="adminInfos[admin.number].lastname.length || adminInfos[admin.number].email.length" 

Check this plunker, it works..

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

Comments

1

You have a logical flaw. ng-required should be or (||) not and (&&). See fork below.

Plunkr

<input type="text" name="admin{{admin.number}}_firstname" ng-model="adminInfos[admin.number].firstname" placeholder="First Name" ng-required="adminInfos[admin.number].lastname.length && adminInfos[admin.number].email.length" />

As a note, please put this complicated UI logic inside a method on the controller :)

Comments

0

You don't need to do ng-required, you can just replace this with simply 'required' to indicate it is necessary in order for the form to be valid. See below:

<!DOCTYPE html>
<html ng-app="test">

<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
</head>

<body ng-controller="testctrl">
  <form name="test_form">
    <input type="text" name="admin{{admin.number}}_firstname" ng-model="adminInfos[admin.number].firstname" placeholder="First Name" required />
    <span ng-show="!system_admins.admin{{admin.number}}_firstname.$pristine && (adminInfos[admin.number].email.length || adminInfos[admin.number].lastname.length) && !adminInfos[admin.number].firstname.length" class="error">First name is required</span>
    <br/>
    <input type="text" name="admin{{admin.number}}_lastname" ng-model="adminInfos[admin.number].lastname" placeholder="Last Name" required />
    <span ng-show="!system_admins.admin{{admin.number}}_lastname.$pristine && (adminInfos[admin.number].email.length ||  adminInfos[admin.number].firstname.length ) && !adminInfos[admin.number].lastname.length" class="error">Last name is required</span>
    <br/>
    <input type="text" name="admin{{admin.number}}_email" ng-model="adminInfos[admin.number].email" placeholder="Email Address" required />
    <span ng-show="!system_admins.admin{{admin.number}}_email.$pristine && !adminInfos[admin.number].email.length && (adminInfos[admin.number].lastname.length || adminInfos[admin.number].firstname.length)" class="error">Email is required</span>

    <br/>
    <br/>

    <button ng-disabled="test_form.$invalid">Continue</button>

  </form>
</body>

</html>

3 Comments

it is required only when one of the fields is and the other two are required. It can be in any order. Otherwise u can hit continue.
Am I understanding correctly, the continue button should be enabled when none or all three are entered? If not, I'm not sure I understand fully the requirements.
A field is required only if there is value in any other fileds, else it's not required.

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.