I have a form with mandatory fields as - 1. First Name 2. Last Name 3. Phone 4. State 5. City 6. Date of Birth I have a button which should toggle to complete/Incomplete based on the form inputs.The button should toggle to 'complete' only when all the fields have proper values.If I remove any of the value again,the button should toggle to Incomplete.
In my ts file-
formCompleteOrIncomplete(person,index) {
if (person.dateOfBirth !== "" && person.phone!== "" &&
(person.firstName !== undefined || person.firstName !== "") && (person.lastName !== undefined || person.lastName !== "") && (person.state !== undefined || person.state !== "") && (person.city !== undefined || person.city !== "" )) {
person.status = true;
}
else {
person.status = false;
}
}
In my html file-
<div class="tag">
<div class="success" *ngIf="person.status">Complete</div>
<div class="failed" *ngIf="!person.status">Incomplete</div>
</div>
form fields-
<md-input required [(ngModel)]="person.firstName" (ngModelChange) = "formCompleteOrIncomplete(person, index)" aria-placeholder="First Name" placeholder="First Name" name="firstName" #firstName="ngModel"></md-input>
On page load the button shows 'Incomplete' as none of the field values are entered and when I enter all the fields, button is getting toggled to 'Complete' but when I try to empty any of the field again, it does not get toggled to 'Incomplete' again.
Also, when i do not include a check for 'undefined' and include only "" check, on page load the button shows 'complete'.NOt sure why is this happening.
Can someone tell me where I am going wrong?