1

I have several inputs in template

<input type="text" class="form-control" name="name" id="name" placeholder="name" title="nanme">
<input type="text" class="form-control" name="surname" id="name" placeholder="surname" title="surname">

in ts file I have variable testVar=false;

I want testVar=true after both inputs will be filled (not empty). How can I do this ?

3 Answers 3

1

You can use Reactive Forms in this way:

template:

<div [formGroup]="profileForm">
      <input formControlName="name" type="text" class="form-control" name="name" id="name" placeholder="name" title="nanme">
      <input formControlName="surname" type="text" class="form-control" name="surname" id="name" placeholder="surname" title="surname">
     <p>
        {{profileForm.status}}
    </p>
</div>

component:

import {Validators, FormBuilder} from '@angular/forms';
profileForm = this.fb.group({
    name: ['', Validators.required],
    surname: ['', Validators.required]
});
constructor(private fb: FormBuilder) { }

And instead of using testVar, you can use profileForm.valid.

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

Comments

0

You can use local reference here #input1 and #input2 and make use of keydown.enter

<input type="text" class="form-control" name="name" id="name" placeholder="name" title="nanme" #input1 (keydown.enter)="checkInputs(input1.value,input2.value)">
<input type="text" class="form-control" name="surname" id="name" placeholder="surname" title="surname" #input2 (keydown.enter)="checkInputs(input1.value,input2.value)">

and then in .ts file:

checkInputs(input1,input2) {
  if(input1.length && input2.length){
  this.testVar=true;
  }else{
  this.testVar=false;
 }
}

Comments

0

Try like this:

Template:

<input type="text" class="form-control" name="name" id="name" placeholder="name" title="nanme" [(ngModel)]="details.name" (ngModelChange)="checkTestValue()">
<input type="text" class="form-control" name="surname" id="name" placeholder="surname" title="surname" [(ngModel)]="details.surname" (ngModelChange)="checkTestValue()">

TS:

 details: any = {};

  checkTestValue() {
    if (this.details.name && this.details.surname) {
      this.testVar = true;
    } else {
      this.testVar = false;
    }
  }

Working Demo

2 Comments

User has mentioned in the question that template has several inputs, not just two. Also not necessarily all the input values can belong to one variable object, which is 'details' in your case.
@Hemendra In that case hecan use Object.keys()

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.